Problem description
In the algorithm of image encoding, a given square matrix is required to perform a Z-glyph scan (Zigzag scan). Given a nxn matrix, the process of zigzag scanning is as follows:
For the following 4x4 matrix,
1539
3756
9464
7313
A sequence of length 16 is obtained after a zigzag scan: 1539739547366413 implement a Z-scan of the program, given a nxn matrix, the output of this matrix
The result of a zigzag scan. Input format
The first line of the input contains an integer n, which represents the size of the matrix. The second line of input to line n+1 each row contains n positive integers, separated by spaces, representing the given matrix.
Output format
The output row, containing nxn integers, separated by spaces, indicates the result of the input matrix after a Z-glyph scan.
Input sample
4
1539
3756
9464
7313
Output sample
1539739547366413
Evaluation Case size and convention 1≤n≤500, The matrix element is a positive integer not exceeding 1000.
Defines four action 1234, which represents 4 ways of moving.
1#include <iostream>2 3 using namespacestd;4 5 intMain () {6 intN;7Cin>>N;8 intS[n][n];9 for(inti =0; i<n;i++)Ten for(intj =0; j<n;j++) OneCin>>S[i][j]; A intX0), Y (0); - intMove=1; - while(x!=n-1|| y!=n-1) the { -cout<<s[x][y]<<" "; - Switch(move) - { + Case 1: -y++; + if(x==0) AMove=2; at Else -Move=4; - Break; - Case 2: -X + +; -y--; in if(y==0&& x!=n-1) -Move=3; to Else if(x==n-1) +Move=1; - Break; the Case 3: *X + +; $ if(y==0)Panax NotoginsengMove=4; - Else theMove=2; + Break; A Case 4: thex--; +y++; - if(x==0&& y!=n-1) $Move=1; $ Else if(y==n-1) -Move=3; - Break; the } - }Wuyicout<<s[n-1][n-1]; the return 0; -}
December 14 CCF Real topic 2-z-zigzag scan