递归打印矩阵

时间:2017-10-17 17:37:56

标签: c algorithm recursion matrix

所以,我试图从我的算法课程书中解决这个练习。练习要求通过将矩阵细分为较小的正方形(矩阵总是2的幂)并打印其中的内容来重复打印矩阵。 必须从左上角然后顺时针打印正方形。

所以一个例子是:

  

0 0 0 0
    0 0 0 0
    0 1 1 1
    0 0 0 0

程序应该打印出来:

  1. 0 0 0 0
    0 0 0 0
    0 1 1 1
    0 0 0 0

  2. 0 0

    0 0

  3. 0 0

    0 0

  4. 0 1
    0 0

  5. 1 1 0 0

  6. 等等。

    到目前为止我尝试过的是这样的事情(使用C检查它是否正确):

    void subdivide(int x1,int x2,int y1,int y2,int n){
        if(n==0){return;}
        else{
            n = n/2;
            subdivide(x1,x2/2,y1,y2/2,n);
            subdivide(x1,x2/2,y2/2,y2,n);
            subdivide(x2/2,x2,y2/2,y2,n);
            subdivide(x2/2,x2,y1,y2/2,n);
        }
        return;
    }
    

    但它并没有完全适用于8x8矩阵,因为它不会到达角(如(4,4)到(6,6),或(6,6)和(8) ,8)。

2 个答案:

答案 0 :(得分:0)

您需要在缩放前调整位置。我不确定您使用的约束(包含或排除),但是如果要显示x范围[4,8],即x索引4(包括)到x索引8(独家),那么这需要分解为[4,6]和[6,8](第一个数字是包容性的,第二个是独家的)。

但你使用的[x1,x2 / 2]是[4,4],[x2 / 2,x2]是[4,8],这是你的原始范围。

假设有一个独占上限,你需要使用:

[x1, (x1+x2)/2)
[(x1+x2)/2, x2)

根据需要产生[4,6]和[6,8]。

答案 1 :(得分:0)

#include <stdio.h>

void dump(int depth, int y1, int x1, int y2, int x2) {
   char indent[depth+1];

   for (int i=0; i<depth; ++i)
      indent[i] = ' ';

   indent[depth] = 0;

   printf("%s(%d,%d)(%d,%d)\n", indent, y1, x1, y2, x2);
}

// Assumes y1<=y2 && x1<=x2
void subdivide(int depth, int y1, int x1, int y2, int x2) {
   dump(depth, y1, x1, y2, x2);
   ++depth;

   int ym = (y2+y1+1)/2;
   int xm = (x2+x1+1)/2;
   if (y1!=y2 && x1!=x2) {
      if (y1==y2) {
         subdivide(depth, y1, x1, y1, xm-1);
         subdivide(depth, y1, xm, y1, x2);
      }
      else if (x1==x2) {
         subdivide(depth, y1, x1, ym-1, x1);
         subdivide(depth, ym, x1, y2,   x1);
      }
      else {
         subdivide(depth, y1, x1, ym-1, xm-1);
         subdivide(depth, y1, xm, ym-1, x2);
         subdivide(depth, ym, x1, y2,   xm-1);
         subdivide(depth, ym, xm, y2,   x2);
      }
   }
}

int main(void) {
   subdivide(0, 1,1, 8,8);
   printf("\n");
   subdivide(0, 1,1, 7,7);
   return 0;
}

输出:

$ gcc -Wall -Wextra -pedantic --std=c99 -o a a.c && a
(0,0)(7,7)
 (0,0)(3,3)
  (0,0)(1,1)
   (0,0)(0,0)
   (0,1)(0,1)
   (1,0)(1,0)
   (1,1)(1,1)
  (0,2)(1,3)
   (0,2)(0,2)
   (0,3)(0,3)
   (1,2)(1,2)
   (1,3)(1,3)
  (2,0)(3,1)
   (2,0)(2,0)
   (2,1)(2,1)
   (3,0)(3,0)
   (3,1)(3,1)
  (2,2)(3,3)
   (2,2)(2,2)
   (2,3)(2,3)
   (3,2)(3,2)
   (3,3)(3,3)
 (0,4)(3,7)
  (0,4)(1,5)
   (0,4)(0,4)
   (0,5)(0,5)
   (1,4)(1,4)
   (1,5)(1,5)
  (0,6)(1,7)
   (0,6)(0,6)
   (0,7)(0,7)
   (1,6)(1,6)
   (1,7)(1,7)
  (2,4)(3,5)
   (2,4)(2,4)
   (2,5)(2,5)
   (3,4)(3,4)
   (3,5)(3,5)
  (2,6)(3,7)
   (2,6)(2,6)
   (2,7)(2,7)
   (3,6)(3,6)
   (3,7)(3,7)
 (4,0)(7,3)
  (4,0)(5,1)
   (4,0)(4,0)
   (4,1)(4,1)
   (5,0)(5,0)
   (5,1)(5,1)
  (4,2)(5,3)
   (4,2)(4,2)
   (4,3)(4,3)
   (5,2)(5,2)
   (5,3)(5,3)
  (6,0)(7,1)
   (6,0)(6,0)
   (6,1)(6,1)
   (7,0)(7,0)
   (7,1)(7,1)
  (6,2)(7,3)
   (6,2)(6,2)
   (6,3)(6,3)
   (7,2)(7,2)
   (7,3)(7,3)
 (4,4)(7,7)
  (4,4)(5,5)
   (4,4)(4,4)
   (4,5)(4,5)
   (5,4)(5,4)
   (5,5)(5,5)
  (4,6)(5,7)
   (4,6)(4,6)
   (4,7)(4,7)
   (5,6)(5,6)
   (5,7)(5,7)
  (6,4)(7,5)
   (6,4)(6,4)
   (6,5)(6,5)
   (7,4)(7,4)
   (7,5)(7,5)
  (6,6)(7,7)
   (6,6)(6,6)
   (6,7)(6,7)
   (7,6)(7,6)
   (7,7)(7,7)

(0,0)(6,6)
 (0,0)(2,2)
  (0,0)(0,0)
  (0,1)(0,2)
   (0,1)(0,1)
   (0,2)(0,2)
  (1,0)(2,0)
   (1,0)(1,0)
   (2,0)(2,0)
  (1,1)(2,2)
   (1,1)(1,1)
   (1,2)(1,2)
   (2,1)(2,1)
   (2,2)(2,2)
 (0,3)(2,6)
  (0,3)(0,4)
   (0,3)(0,3)
   (0,4)(0,4)
  (0,5)(0,6)
   (0,5)(0,5)
   (0,6)(0,6)
  (1,3)(2,4)
   (1,3)(1,3)
   (1,4)(1,4)
   (2,3)(2,3)
   (2,4)(2,4)
  (1,5)(2,6)
   (1,5)(1,5)
   (1,6)(1,6)
   (2,5)(2,5)
   (2,6)(2,6)
 (3,0)(6,2)
  (3,0)(4,0)
   (3,0)(3,0)
   (4,0)(4,0)
  (3,1)(4,2)
   (3,1)(3,1)
   (3,2)(3,2)
   (4,1)(4,1)
   (4,2)(4,2)
  (5,0)(6,0)
   (5,0)(5,0)
   (6,0)(6,0)
  (5,1)(6,2)
   (5,1)(5,1)
   (5,2)(5,2)
   (6,1)(6,1)
   (6,2)(6,2)
 (3,3)(6,6)
  (3,3)(4,4)
   (3,3)(3,3)
   (3,4)(3,4)
   (4,3)(4,3)
   (4,4)(4,4)
  (3,5)(4,6)
   (3,5)(3,5)
   (3,6)(3,6)
   (4,5)(4,5)
   (4,6)(4,6)
  (5,3)(6,4)
   (5,3)(5,3)
   (5,4)(5,4)
   (6,3)(6,3)
   (6,4)(6,4)
  (5,5)(6,6)
   (5,5)(5,5)
   (5,6)(5,6)
   (6,5)(6,5)
   (6,6)(6,6)