Zigzag Scan
Time limit: |
2.0s |
Memory Limit: |
256.0MB |
Problem Descriptionin 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, 1 5 3 9 3 7 5 6 9 4 6 4 7 3 1 3 a sequence with a length of 16 is obtained after a zigzag scan: 1 5 3 9 7 3 9 5 4 7 3 6 6 4 1 3 implement a Z-scan program, given a nxn matrix, which outputs the result of a Z-scan of the matrix. Input Formatthe 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 FormatThe output row, containing nxn integers, separated by spaces, indicates the result of the input matrix after a Z-glyph scan. Sample Input4 1 5 3 9 3 7 5 6 9 4 6 4 7 3 1 3Sample Output1 5 3 9 7 3 9 5 4 7 3 6 6 4 1 3Measuring use case size and conventions1≤n≤500, The matrix element is a positive integer not exceeding 1000. |
Problem solving: Look, it's like drawing a circle ...
1#include <iostream>2#include <cstring>3 using namespacestd;4 Const intMAXN = -;5 intD[maxn][maxn],n;6 Const intdir[2][2] = {-1,1,1,-1};7 BOOLIsIn (intXinty) {8 returnX >=0&& x < n && y >=0&& y <N;9 }Ten intMain () { One while(~SCANF ("%d",&N)) { A for(inti =0; I < n; ++i) - for(intj =0; J < N; ++j) -scanf"%d", d[i]+j); the intm = n + N-1; - BOOLFlag =true, o =false; - for(inti =0; I < m; ++i,flag =!flag) { - for(intj = Flag?0I Flag? (J <= i):(J >=0); flag?++j:--j) { + if(IsIn (ij,j)) { - if(o) Putchar (' '); +printf"%d", d[i-J] [j]); Ao =true; at } - } - } -Putchar ('\ n'); - } - return 0; in}
View Code
CCF Z-Glyph scan