Serpentine Matrix (Niuke.com) Nanny Level Detailed Explanation

1. Topic:

Description: Give you an integer n and output a serpentine matrix of n*n.

Input description: Enter a line containing an integer n

Output description: Output n lines, each line contains n positive integers, separated by spaces.

(1<=n<=1000)

Example:

Input: 4

Output: 1 2 6 7

           3    5    8    13

           4    9    12   14

          10   11   15    16

2. Topic analysis: 

(1) Clarify the composition of the serpentine matrix:

From the above figure, we can find that there are only two directions of the matrix:

1. From right to left (bottom)

2. From left to right (top)

(2) Logical reasoning:        

Therefore, I use the pos variable to represent the direction, where 1 represents the upper right and right, and -1 represents the lower left and lower.
Use rows to irepresent columns .j

There are 6 types of exercise :

  1. Hitting the upper boundary ( i.e. i equals 1 and j is less than n ): the column increases by 1 and the row remains unchanged----move one space to the right
  2. Hitting the left boundary ( i.e. j equals 1 and i is less than n ): the row increases by 1 and the column remains unchanged----move down one space
  3. Hitting the right boundary ( that is, j equals n ): the row increases by 1 and the column remains unchanged----move down one space
  4. When the lower boundary is encountered ( that is, i equals n ): the column increases by 1 and the row remains unchanged----move one space to the right

The above four types of movement are above the boundary. Only right and downward

     5. Except for the above four boundary conditions, it is to move to the lower left in the middle of the boundary

     6. Except for the above four boundary conditions, move to the upper right in the middle of the boundary

The last two move within the boundary only to the upper right and lower left  .

3. Conversion code:

#include <stdio.h>

int array[1001][1001] = { 0 };

int main()
{
    int n = 0;
    scanf("%d",&n);
    int i = 1, j = 1, k = 0,pos = 1; //pos代表方向移动   1代表右上   -1代表左下
    array[i][j] = 1;
    for (k = 2; k <= n*n; k++)
    {
        if (i == 1 && j <n && pos ==1) //碰到上边界,列增加1,行不变     此后行增,列减
        {
            array[i][++j] = k;
            pos = -1;                 //方向注意
        }
        else if(j==1 && i<n && pos == -1)//碰到左边界,行增加1,列不变     此后行减,列增
        {
            array[++i][j] = k;
            pos = 1;                     //方向注意               
        }
        else if (j == n && pos==1) //碰到左边界,行增加1,列不变     此后行增,列减
        {
            array[++i][j] = k;
            pos = -1;                     //方向注意
        }
        else if (i == n && pos ==-1) //碰到右边界,列增加1,行不变    此后行减,列增
        {
            array[i][++j] = k;
            pos = 1;
        }
        else if (pos == 1)         //除去上面的边界情况,就是中间移动过程
        {
            array[--i][++j] = k;
        }
        else if(pos == -1)
        {
            array[++i][--j] = k;
        }
    }
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
            printf("%d ",array[i][j]);
        printf("\n");
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/2201_75998194/article/details/131443073