Reverse order and serpentine output matrix

Posted by run_GMoney on Sat, 30 Nov 2019 19:50:07 +0100

Reverse order and serpentine output matrix

Reverse order

Input n, get the n*n matrix, output the element values anticlockwise, and separate them with spaces.
For example, n=4, the matrix is
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The output content is: 1 23 4 8 12 16 15 13 9 5 6 7 11 10.

Analysis

First of all, this is a boundary problem. From the inside to the outside, we can understand it as a motion problem, that is, from Matrix[0][0], around the periphery of the matrix to the inside of the matrix.
We can define four directions, namely 0 / 1 / 2 / 3 (right, bottom, left, top). Then define Mark[n][n]=1. When a value is output, Mark[n][n]=0;
Adjust the direction by judging the boundary.

Code block

int matrix[N][N];
int mark[N][N];
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        int t=1;
        int i;
        //Initialization
        for(i=0;i<n;i++){
            for(int j=0;j<n;j++){
                matrix[i][j]=(t++);
            }
        }
        for(i=0;i<n;i++){
            for(int j=0;j<n;j++){
                mark[i][j]=1;
            }
        }
        printf("\n");
        int count=n*n;
        int go=0,x=0,y=0;
        while(count--){
            switch(go){
                case 0:     //right
                    printf("%d ",matrix[x][y]);
                    mark[x][y]=0;
                    if(mark[x][y+1]==0){
                       go=1;
                        x++;
                    }else{
                        y++;
                    }
                    break;
                case 1:     //lower
                    printf("%d ",matrix[x][y]);
                    mark[x][y]=0;
                    if(mark[x+1][y]==0){
                        go=2;
                        y--;
                    }else{
                        x++;
                    }
                    break;
                case 2:     //Left
                    printf("%d ",matrix[x][y]);
                    mark[x][y]=0;
                    if(mark[x][y-1]==0){
                        go=3;
                        x--;
                    }else{
                        y--;
                    }
                    break;
                case 3:     //upper
                    printf("%d ",matrix[x][y]);
                    mark[x][y]=0;
                    if(mark[x-1][y]==0){
                        go=0;
                        y++;
                    }else{
                        x--;
                    }
                    break;
            }
        }
        printf("\n");

    }
    return 0;
}

Serpentine

Input n, get n*n matrix, snake output each element value, and space.
For example, n=4, the matrix is
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The output is: 1 2 5 9 6 3 4 7 10 13 11 8 12 15 16

Analysis

This is also a boundary problem. The matrix output has four directions, i.e. 0 / 1 / 2 / 3 (right, bottom left, bottom right, top right). When it reaches the boundary, it only needs to judge the next big direction.

Code block

int count=n*n-1;
        int go=0,x=0,y=0;
        printf("%d ",matrix[x][y]);
        mark[x][y]=0;
        while(count--){
            switch(go){
                case 0:     //right
                y++;
                printf("%d ",matrix[x][y]);
                mark[x][y]=0;
                if(x==0){
                    go=1;
                }else if(x==n-1){
                    go=3;
                }
                break;

            case 1:     //Left lower
                if(x<n-1 && y>0){
                    x++;
                    y--;
                }
                printf("%d ",matrix[x][y]);
                if(y==0 && x==n-1){
                    go=0;
                }else if(y==0){
                    go=2;
                }else if(x==n-1){
                    go=0;
                }
                break;

            case 2:     //lower
                x++;
                printf("%d ",matrix[x][y]);
                mark[x][y]=0;
                if(y==0 || x==n-1){
                    go=3;
                }else if(y==n-1){
                    go=1;
                }
                break;

            case 3:     //Right upper
                if(x>0 && y<n-1){
                    x--;
                    y++;
                }
                printf("%d ",matrix[x][y]);
                mark[x][y]=0;
                if(y==n-1 && x==0){
                    go=2;
                }else if(y==n-1){
                    go=2;
                }else if(x==0){
                    go=0;
                }
                break;

            }

Topics: C