Topics
Input matrix size n, output zigzag matrix.
The zigzag order is shown
Input
5
Output
0 1 5 6 14 2 4 7 13 15 3 8 12 16 21 9 11 17 20 2210 18 19 23 24
parsing 1
This method is the corresponding element value for the calculated order position.
Suppose I is the number of rows, J is the number of columns, and each slash subscript (i+j) is a constant, recorded as S (s=i+j).
Processing is divided into the upper and lower triangular two parts processing.
For the upper triangle, the number of elements on each slash is one more than the previous slash, and the first element on each slash represents the number of elements before the slash. Using the arithmetic progression summation formula, the value of the first element of each slash is (1 + s) * S/2. On an odd slash, the element is incremented to the right, and any element on it can be represented as s* (s+1)/2+j, and any element on an even slash can be represented as s* (s+1)/2+i.
For the lower triangle, the number of elements on the slash is decremented. Conversely, it can be seen as incremental, you can still apply arithmetic progression summation formula, find the number of remaining elements, with the total number of elements minus the number of elements remaining is the first element value on the current slash. The number of elements is n*n, and the remaining number of elements is M * (M + 1)/2, where m= (n-1-i) + (N-1-J). The value of the first element of the slash is n*n-m* (m+1)/2.
Code 1
#include <iostream>#include <iomanip>#include <vector>using namespace STD;intMain () {intNCin>> N;// vector<int>VEC (N*n);intS,m,smax = n * N; for(inti =0; i < N; i++) { for(intj =0; J < N; J + +) {s = i + j;if(S < N) {Vec[i * N + j] = S * (s +1) /2+ ((s%2==0) ? J:I); }Else{m = (N-1-i) + (N-1-j); Vec[i * N + j] = Smax-m * (M +1) /2-(N-((s)2==0) ? J:I)); } } } for(inti =0; i < N; i++) { for(intj =0; J < N; J + +) {cout<< SETW (4) << Vec[i * N + j]; }cout<< Endl; }return 0;}
Parsing 2
This method is to find the corresponding position of the order element
The ergodic trend of arbitrary elements in zigzag matrices is divided into boundary direction and non-boundary trend M[i][j
Non-boundary trend
Non-boundary trend is relatively simple, if the two-dimensional array of elements m[i][j] of the horizontal and i+j are even, then the Traverse path in the matrix is moved to the upper right corner of a grid; otherwise, if the i+j is odd, then the Traverse path in the matrix is moved in the lower left corner of a grid.
Border direction
When n is odd, it is divided into upper and lower two triangular matrices
Upper triangular matrix
If the coordinate j in the two-dimensional array m[i][j] is an even number, then the Traverse path in the matrix moves horizontally to the right by one square.
If the element in the two-dimensional array m[i][j] is an odd number of coordinates I, then the Traverse path in the matrix moves vertically down one grid.
Lower triangular matrix
If the coordinate j in the two-dimensional array m[i][j] is odd, then the Traverse path in the matrix moves horizontally to the right by one square.
If the element in the two-dimensional array m[i][j] coordinates i is an even number, then the Traverse path in the matrix moves vertically down one grid.
When n is an even number,
If the coordinates j in the two-dimensional array m[i][j] is even, and i=0 or i=7, then the Traverse path in the matrix moves horizontally to the right by one square.
If the element in the two-dimensional array m[i][j] coordinates i is odd, and j=0 or j=7, then the Traverse path in the matrix is moving vertically down one grid.
Code 2
#include <iostream>#include <iomanip>#include <vector>using namespace STD;intMain () {intNCin>> N; vector<int>VEC (N*n);intx=0, y=0; for(inti =0; i < N; i++) { for(intj =0; J < N; J + +) {vec[x * n + y] = i * n +j;//boundary steering divided into odd and even two cases //Odd cases are divided into upper and lower triangles for processing ifN2) {//Upper triangle if((x + y) < N-1) {if(x = =0&& y%2==0) {y++;Continue; }if(Y = =0&& x%2==1) {x + +;Continue; } }//Lower triangle Else{if(x = = N-1&& y%2==1) {y++;Continue; }if(y = = N-1&& x%2==0) {x + +;Continue; } } }//Even cases can be processed directly Else{if((x = = N-1|| x = =0) && y%2==0) {y++;Continue; }if(y = = N-1|| y = =0) && x%2==1) {x + +;Continue; } }//non-boundary processing if((x + y)%2==0) {x--; y++; }Else{x + +; y--; } } } for(inti =0; i < N; i++) { for(intj =0; J < N; J + +) {cout<< SETW (4) << Vec[i * N + j]; }cout<< Endl; }return 0;}
Geek Programming Exercises--zigzag matrices