How to print a matrix
Print the matrix in a clockwise direction
How to print the elements of a matrix clockwise, For example: If you enter the following matrix:
1 2 3 4
5 6 7 8
9 10 11   12
13 14 15 16
Then the numbers are printed in sequence:1, 2, 3, 4, 8, ten,,, 9, 5, 6,, one.
Idea: Use a similar depth search method to do, every time in one direction, if you can not go again clockwise turn.
intarray[ +][ +];intcanuse[ +][ +];intstep[4][2] = {{0,1}, {1,0}, {0, -1}, {-1,0}};voidScan (intXintYintDirectintMintN) { inti; Canuse[x][y]=0; printf ("%d", Array[x][y]); for(i =0; I <4; i++) { intJ = (direct + i)%4; inttx = x + step[j][0]; intty = y + step[j][1]; if(0<= TX && TX < M &&0<= ty && ty < n &&Canuse[tx][ty]) {Scan (TX, Ty, J, M, N); } } }intMain () {intm =4, n =4; intI, J, v=1; for(i=0; i<m; i++) { for(j=0; j<n; J + +) {Array[i][j]= v++; CANUSE[I][J]=1; }} Scan (0,0,0, M, N); printf ("\ n");}
traverse a matrix in a diagonal direction
How to traverse the elements of a matrix in a diagonal direction, For example: If you enter the following matrix:
1 2 3 4
5 6 7 8
9 10 &NBSP;&NBSP;&NBSP;&NBSP;11&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;12
13 14 15 16
The numbers are printed in turn: 1, 2, 5, 9, 6, 3, 4, 7, 10, 13, 14, 11, 8, 12, 15, 16
Ideas : The point on the same line, whose coordinates are x+y necessarily equal
An even number of times (counting starting from 0) moves along the upper right, while odd times moves along the lower left.
intMain () {intM =4, N =4; intMatrix[m][n]; intI, J, v=1; for(i=0; i<m; i++) { for(j=0; j<n; J + +) {Matrix[i][j]= v++; } } for(i =0; I < M + N; i++) { ifI2==0) { intj = i < M? i = J-1; intK = i-J; while(J >=0&& K <N) {printf ("%d,", matrix[j--][k++]); } } Else { intK = i < N? I:n-1; intj = i-K; while(k >=0&& J <M) {printf ("%d,", matrix[j++][k--]); }}} printf ("\ n");}
Print Spiral Matrix
Here is a spiral queue:
73 74 75 76 77 78 79 80Bayi
72 43 44 45 46 47 48 the50
71 42 21 22 23 24 -26 51
70 41 20) 7 8 9 10 27 52
69 40 19 6 1 2 11 28 53
68 39 18 5 4 3 12 29 54
67 38 17 16 15 14 13 30 55
66 37 36 35 34 33 32 31 56
65 64 63 62 61 60 59 58 57
See above the pattern of the number arrangement, set 1 coordinates is (0,0), the x direction to the right is positive, the y direction downward is positive. For example: 7 coordinates ( -1,-1), 2 coordinates (1,0), and 3 coordinates (.). The programming implementation input any point coordinates (x, y), output the corresponding number, or enter any number, output the coordinates of the number.
Analysis: The law can be seen, the problem is how to use it. It's obvious that this queue is scaled out clockwise, and we can think of it as one layer at a stretch. The No. 0 level is defined as the middle 1, the 1th layer is 2 to 9, the 2nd layer is 10 to 25, notice 1, 9, and 、...... Isn't that the square number? And it's a continuous odd number (1, 3, 5 、...... ) of the square number. These numbers are also related to the number of layers, and you can figure out the number of 2t-1 ^2 within the T layer, and the T layer will continue to spiral outward from [(2t-1) ^2] + 1. Given the coordinates (x, y), how do you know if the point is on the first layer? Number of layers T = max (|x|,|y|).
Know the number of layers, and then to do a lot more, then we know that the point of demand must be in the T layer of this circle, along the number just down. It is important to note that the spiral queue is not necessarily the same as the direction of the axis of the numerical growth. We can be divided into four kinds of situations-upper, lower, left, right-or-east, south, west, north, respectively, on four sides to analyze.
East | right: x = = T, queue growth direction and Y axis consistent, positive orient (y = 0) value is (2t-1) ^2 + t, so v = (2t-1) ^2 + t + Y
South | lower: y = = t, queue growth direction and x axis opposite, positive south (x = 0) value is (2t-1) ^2 + 3t, so v = (2t-1) ^2 + 3t-x
West | left: x = =-T, queue growth direction and Y axis opposite, positive West (y = 0) value (2t-1) ^2 + 5t, so v = (2t-1) ^2 + 5t-y
North | upper: Y = =-T, queue growth direction and X axis consistent, north direction (x = 0) value is (2t-1) ^2 + 7t, so v = (2t-1) ^2 + 7t + x
In fact, there is another important point, otherwise there will be problems. The other three sides are OK, but on the east (right) line, the queue increase does not exactly match the formula! Notice that the northeast corner (upper right corner) is the last number of this layer, and then down is the first number of this layer, which of course does not meet the East line formula AH. So we put the East Line's judgment at the end (it just needs to be on the North Line), so the northeast corner is always considered the point on the North Line.
The following is an illustration of the T-layer:
//Spiral Queue Problem#include<iostream>using namespacestd; #defineMax (a) (a) > (b)? (a): (b))#defineABS (a) ((a) >0? (a):-(a))#defineSquare (a) ((a) * (a))//input coordinates, output the corresponding numberintSpiral_queue (intXinty) { intVal//the value that corresponds to the coordinate intt = MAX (ABS (x), ABS (y));//The number of layers in which the coordinates reside if(y = =-T)//North (north of the judging branch to the east before, this is for the northeast corner maximum consideration)val = Square (2*t-1)+7*t+x; Else if(y = = t)//Southval = Square (2*t-1)+3*t-x; Else if(x = =-T)//West Sideval = Square (2*t-1)+5*t-y; Else if(x = = t)//East Sideval = Square (2*t-1) +t+y; returnVal; } intMain () {intx, y; Const intN =4;//number of layers to print for(Y=-n; y<=n; y++){ for(x=-n; x<=n; x + +) cout<<spiral_queue (x, y) <<" "; cout<<endl;//Press the Y layer to print and wrap } return 0; }
How to print a matrix