实验室师兄找工作时在有道遇到的一道笔试题:打印双螺旋矩阵。
双螺旋矩阵的定义如下,矩阵的最中心是1,往上是2,右拐3,向下4,然后依次5、6,7...构成一条顺序增大的螺旋线,此外,如果从中心往下走的话,也是一条对称的螺旋线。题目是给定一个矩阵维度N,将其打印出来,示例如下。要求在纸上把代码写完整,时间半小时左右。
25 14 15 16 17 18 19
24 13 6 7 8 9 20
23 12 5 2 3 10 21
22 11 4 1 4 11 22
21 10 3 2 5 12 23
20 9 8 7 6 13 24
19 18 17 16 15 14 25
看上去似乎挺简单,但是做的时候还是遇到了一些麻烦,最后参考师兄的思路,我给出了下面的实现。基本思路是,先打印中心,然后一圈一圈地将外围矩阵打印出来。打印每圈的时候,先分别计算出左上角和右下角的坐标(Pos)和值,然后沿着螺旋线的方向,从左上角出发打印这一圈矩阵的上边和右边;从右下角出发打印下边和左边。代码如下,能够正确运行,欢迎提出意见。
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- typedef struct {
- int row;
- int column;
- } Position;
- void print_screws_matrix( int size )
- {
- int **matrix = (int **) malloc( size*sizeof(int*) );
- int i,j; // just for loop
- for( i = 0; i < size; ++i )
- matrix[i] = (int *) malloc( size*sizeof(int) );
- /* initialize matrix center */
- Position centerPos;
- centerPos.row = ( size - 1 )/2;
- centerPos.column = ( size - 1 )/2;
- matrix[ centerPos.row ][ centerPos.column ] = 1;
- int screwNum;
- /* screwNum of matrix center is 0 */
- for( screwNum = 1; screwNum <= (size-1)/2; ++screwNum ) {
- /* get the beginning positions for each screw */
- Position screwUpRightPos, screwDownLeftPos;
- screwUpRightPos.row = centerPos.row - screwNum;
- screwUpRightPos.column = centerPos.column - screwNum + 1;
- screwDownLeftPos.row = centerPos.row + screwNum;
- screwDownLeftPos.column = centerPos.column + screwNum - 1;
- /* initialize beginning positions */
- matrix[ screwUpRightPos.row ][ screwUpRightPos.column ]
- = matrix[ screwUpRightPos.row + 1 ][ screwUpRightPos.column ] + 1;
- matrix[ screwDownLeftPos.row ][ screwDownLeftPos.column ]
- = matrix[ screwDownLeftPos.row - 1 ][ screwDownLeftPos.column ] + 1;
- /* calculate up and down sides of screw */
- for( i = 0; i < 2*screwNum - 1; ++i ) {
- screwUpRightPos.column++;
- matrix[ screwUpRightPos.row ][ screwUpRightPos.column ]
- = matrix[ screwUpRightPos.row][ screwUpRightPos.column - 1 ] + 1;
- /* calculate up and down sides of screw */
- for( i = 0; i < 2*screwNum - 1; ++i ) {
- screwUpRightPos.column++;
- matrix[ screwUpRightPos.row ][ screwUpRightPos.column ]
- = matrix[ screwUpRightPos.row][ screwUpRightPos.column - 1 ] + 1;
- screwDownLeftPos.column--;
- matrix[ screwDownLeftPos.row ][ screwDownLeftPos.column ]
- = matrix[ screwDownLeftPos.row ][ screwDownLeftPos.column + 1 ] + 1;
- }
- /* calculate right and left sides of screw */
- for( i = 0; i < 2*screwNum; ++i ) {
- screwUpRightPos.row++;
- matrix[ screwUpRightPos.row ][ screwUpRightPos.column ]
- = matrix[ screwUpRightPos.row - 1][ screwUpRightPos.column ] + 1;
- screwDownLeftPos.row--;
- matrix[ screwDownLeftPos.row ][ screwDownLeftPos.column ]
- = matrix[ screwDownLeftPos.row + 1][ screwDownLeftPos.column ] + 1;
- }
- }
- /* print matrix */
- for( i = 0; i < size; ++i ) {
- for( j = 0; j < size; ++j )
- printf( "%4d ",matrix[ i ][ j ]);
- printf( "/n" );
- }
- for( i = 0; i < size; ++i )
- free( matrix[i] );
- free( matrix );
- }
- int main()
- {
- print_screws_matrix( 21 );
- return 0;
- }
参考:http://blog.csdn.net/dennis101/article/details/3053739