螺旋打印矩阵

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了螺旋打印矩阵相关的知识,希望对你有一定的参考价值。

题目: 对于一个N*M矩阵,请顺时针螺旋打印输出;

如 3 * 4 矩阵,

1   2   3  4

10 11 12 5

9    8   7  6

输出: 1 2 3 4 5 6 7 8 9 10 11 12

 

  1 #include <stdio.h>
  2 // nomally print matric
  3 void print_matric(const int *m, int r, int c) {
  4     int i, j;
  5     printf("%d * %d matric:\n", r, c);
  6     for(i = 0; i < r; i++) {
  7             for(j = 0; j < c; j++) {
  8                     printf("%d\t", *(m + i * c + j));
  9             }
 10             printf("\n");
 11     }
 12     return;
 13 }
 14 void clockwise_spiral_print_matric(const int *m, int r, int c) {
 15     int x0 = 0, x1 = c;
 16     int y0 = 0, y1 = r;
 17     int i;
 18     if(m == NULL || r <= 0 || c <= 0) {
 19         printf("empty matric\n");
 20         return;
 21     }
 22 
 23     print_matric(m, r, c);
 24     printf("clockwise spiral print matrix\n");
 25     
 26     while(x0 < x1 && y0 < y1) {
 27         // print top line
 28         for(i = x0; i < x1; i++) {
 29                 printf("%d ", *(m + x0 * c + i));
 30         }
 31         y0++;
 32         
 33         // print right col
 34         for(i = y0; i < y1 && x0 < x1 && y0 < y1; i++) {
 35                 printf("%d ", *(m + i * c + x1 - 1));
 36         }
 37         x1--;
 38 
 39         // print bottom line
 40         for(i = x1 - 1; i >= x0 && x0 < x1 && y0 < y1; i--) {
 41                 printf("%d ", *(m + (y1 - 1) * c + i));
 42         }
 43         y1--;
 44         
 45         // print left col
 46         for(i = y1 - 1; i >= y0 && x0 < x1 && y0 < y1; i--) {
 47                 printf("%d ", *(m + i * c + x0));
 48         }
 49         x0++;
 50     }
 51     printf("\n\n");
 52     return;
 53 }
 54 // for test
 55 int main() {
 56     int r1 = 1;
 57     int c1 = 1;
 58     int m1[1][1] = {{1}};
 59 
 60     int r2 = 1;
 61     int c2 = 3;
 62     int m2[1][3] = {{1,2,3}};
 63 
 64     int r3 = 4;
 65     int c3 = 1;
 66     int m3[4][1] = {{1}, {2}, {3}, {4}};
 67 
 68     int r4 = 2;
 69     int c4 = 2;
 70     int m4[2][2] = {{1,2}, {4,3}};
 71 
 72     int r5 = 3;
 73     int c5 = 3;
 74     int m5[3][3] = {{1,2,3}, {8,9,4}, {7,6,5}};
 75 
 76     int r6 = 4;
 77     int c6 = 4;
 78     int m6[4][4] = {{1,2,3,4}, {12,13,14,5}, {11,16,15,6}, {10,9,8,7}};
 79     
 80     int r7 = 3;
 81     int c7 = 4;
 82     int m7[3][4] = {{1,2,3,4}, {10,11,12,5}, {9,8,7,6}};
 83 
 84     int r8 = 4;
 85     int c8 = 3;
 86     int m8[4][3] = {{1,2,3}, {10,11,4}, {9,12,5}, {8,7,6}};
 87 
 88     int r9 = 4;
 89     int c9 = 5;
 90     int m9[4][5] = {{1,2,3,4,5}, {14,15,16,17,6}, {13,20,19,18,7}, {12,11,10,9,8}};
 91 
 92     int r10 = 5;
 93     int c10 = 4;
 94     int m10[5][4] = {{1,2,3,4}, {14,15,16,5}, {13,20,17,6}, {12,19,18,7}, {11,10,9,8}};
 95 
 96     clockwise_spiral_print_matric((const int *)m1, r1, c1);
 97     clockwise_spiral_print_matric((const int *)m2, r2, c2);
 98     clockwise_spiral_print_matric((const int *)m3, r3, c3);
 99     clockwise_spiral_print_matric((const int *)m4, r4, c4);
100     clockwise_spiral_print_matric((const int *)m5, r5, c5);
101     clockwise_spiral_print_matric((const int *)m6, r6, c6);
102     clockwise_spiral_print_matric((const int *)m7, r7, c7);
103     clockwise_spiral_print_matric((const int *)m8, r8, c8);
104     clockwise_spiral_print_matric((const int *)m9, r9, c9);
105     clockwise_spiral_print_matric((const int *)m10, r10, c10);
106     
107     return 0;
108 }
109         

 

以上是关于螺旋打印矩阵的主要内容,如果未能解决你的问题,请参考以下文章

以螺旋顺序打印二维数组

在Python中理解顺时针螺旋的打印矩阵

编程题之打印二维螺旋矩阵

C版——打印螺旋矩阵

剑指 Offer 29. 顺时针打印矩阵-模拟循环(59. 螺旋矩阵 II)

54. 螺旋矩阵