CCF 201412-2 zigzag scanning

Test question No.: 201412-2 Test question Name: zigzag scanning Time limit: 2.0s memory limit: 256.0MB Problem Description: In the image coding algor...
Problem Description:
Input format
Output format
sample input
sample output
Code:
Test question No.: 201412-2 Test question Name: zigzag scanning

Time limit: 2.0s memory limit: 256.0MB

Problem Description:

In the image coding algorithm, a given square matrix needs to be zigzag scan. Given a matrix of n × n, the process of zigzag scanning is as follows:

The length of the sequence is 16 after Z-scan
1 5 3 9 7 3 9 5 4 7 3 6 6 4 1 3
Please implement a zigzag scanning program, give a matrix of n × n, and output the result of zigzag scanning on this matrix.

Input format

The first row of the input contains an integer n that represents the size of the matrix. The second input line to the n+1 line contains n positive integers, separated by spaces, representing the given matrix.

Output format

The output line contains n × n integers, separated by spaces, and represents the result of the input matrix after Z-scan.

sample input

4
1 5 3 9
3 7 5 6
9 4 6 4
7 3 1 3

sample output

1 5 3 9 7 3 9 5 4 7 3 6 6 4 1 3

Scale and convention of evaluation case: 1 ≤ n ≤ 500, matrix element is a positive integer of no more than 1000.

Code:

#include <cstring> #include <iostream> #include <algorithm> #include <cstdio> #include <cmath> using namespace std; int input[500][500]; int main() { int n; int change[500][500]; cin>>n; for(int i=0; i<n; i++) for(int j=0; j<n; j++) cin>>input[i][j]; for(int i=0; i<n; i++) for(int j=0; j<n; j++) change[i][j] = input[i][j-i]; for(int i=0; i<n-1; i++) for(int j=0; j<=i; j++) change[i+1][j] = input[i+1][n-1-j]; for(int i=1; i<n; i++) { int temp; for(int j=0; j<i/2; j++) { temp = change[i][j]; change[i][j] = change[i][i-1-j]; change[i][i-1-j] = temp; } } int flag = 0; for(int i=0; i<n; i++) { flag++; if(flag%2) { int temp; for(int j=0; j<flag/2; j++) { temp = change[j][i]; change[j][i] = change[flag-1-j][i]; change[flag-1-j][i] = temp; } } } for(int i=0; i<n-1; i++) { flag++; if(flag%2) { int temp; for(int j=0; j<(n-1-i)/2; j++) { temp = change[i+j+1][i]; change[i+j+1][i] = change[n-1-j][i]; change[n-1-j][i] = temp; } } } for(int k=0; k<n; k++) for(int j=0; j<k+1; j++) cout<<change[j][k]<<" "; for(int k=0; k<n-1; k++) for(int j=0; j<n-k-1; j++) cout<<change[j+k+1][k]<<" "; return 0; }

If you have any questions, please comment!

2 December 2019, 01:47 | Views: 2804

Add new comment

For adding a comment, please log in
or create account

0 comments