CCF-CSP 201412-2 Z-shaped scanning

Problem description
  In the image coding algorithm, a given square matrix needs to be Zigzag Scanned (Zigzag Scan). Given an n × n matrix, the zigzag scanning process is shown in the following figure:
This question describes the picture
  For the following 4 × 4 matrix,
  1 5 3 9
  3 7 5 6
  9 4 6 4
  7 3 1 3
  zigzag scan it Then get a sequence of length 16:
  1 5 3 9 7 3 9 5 4 7 3 6 6 4 1 3
  Please implement a zigzag scanning program. Given an n × n matrix, the output performs zigzag scanning on this matrix the result of.
Input format
  The first line of input contains an integer n, which represents the size of the matrix.
  Each line from the second line to the n + 1 line of the input contains n positive integers, separated by spaces, to represent the given matrix.
Output format
  Output one line, containing n × n integers, separated by spaces, indicating the result of the input matrix after zigzag scanning.
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
Evaluation use case scale and convention
  1≤n≤500, The matrix elements are positive integers not exceeding 1000.

Summary of experience:
Hard core simulation: This question can be printed in two parts, the upper triangle is printed first, and then the lower triangle is printed.
Starting from the upper left corner, it is a problem to traverse a hypotenuse every time, and whether each hypotenuse traverses obliquely up or down. As long as you think about it, you can solve it.
It can be seen from the matrix that for the upper triangle, regardless of whether the side length n of the matrix is ​​even or odd, the hypotenuse in the upper right corner (counting from 0), the even hypotenuse is oblique upward traversal, and the odd oblique edge is oblique downward traversal.
The lower triangle in the matrix needs to be divided into even side length matrix and odd side length matrix.
When the matrix length n is even, the first hypotenuse of the lower triangle (that is, the first hypotenuse after the middle left-bottom to upper-right diagonal in the matrix) is obliquely traversed, while the matrix with odd-numbered sides is opposite . Then iterate in two times.

C ++ code:

#include<bits/stdc++.h>
using namespace std;
int main() {
	int n;
	scanf("%d",&n);
	int num[n][n];
	for(int i=0; i<n; i++) {
		for(int j=0; j<n; j++) {
			scanf("%d",&num[i][j]);
		}
	}
	printf("%d",num[0][0]);
	for(int i=1; i<n; i++) { //打印上三角
		if(0 == i%2) { //斜边是偶数斜边,斜上遍历
			for(int j=0,k=i; j<=i; j++,k--) {
				printf(" %d",num[k][j]);
			}
		} else { //斜边是奇数斜边,斜下遍历
			for(int j=i,k=0; k<=i; j--,k++) {
				printf(" %d",num[k][j]);
			}
		}
	}
	// 打印下三角
	if(0 == n%2) { //边长是偶数的矩阵
		for(int i=1; i<n; i++) {
			if(0 != i%2) {
				for(int j=i,k=n-1; j<n; j++,k--) {
					printf(" %d",num[k][j]);
				}
			} else {
				for(int j=n-1,k=i; k<n; k++,j--) {
					printf(" %d",num[k][j]);
				}
			}
		}
	} else { //边长是奇数的矩阵
		for(int i=1; i<n; i++) {
			if(0 == i%2) {
				for(int j=i,k=n-1; j<n; j++,k--) {
					printf(" %d",num[k][j]);
				}
			} else {
				for(int j=n-1,k=i; k<n; k++,j--) {
					printf(" %d",num[k][j]);
				}
			}
		}
	}
	return 0;
}
Published 111 original articles · won praise 2 · Views 3533

Guess you like

Origin blog.csdn.net/m0_38088647/article/details/100632490