Problem DescriptionThe back shape is taken by the number of edges along the matrix, if the current direction is numerous or has been taken, then left 90 degrees. Starts at the upper-left corner of the matrix, in a downward direction.
Input FormatThe first line of input is two positive integers of not more than 200 m, N, which represent the rows and columns of the matrix. Next the M line is n integers per row, representing the matrix.
output FormatOutput is only one row, the number of total mn, the input matrix is the return of the results obtained. The number is separated by a space, and there is no extra space at the end of the line.
Sample Input
3 3 1 2 3 4 5 6 7 8 9
Sample Output
1 4 7 8 9 6 3 2 5
Sample Input
3 2 1 2 3 4 5 6
Sample Output
1 3 5 6 4 2
Kam SAC 1
Looping, marking.
Kam SAC 2
Use multiple loops to process, or you can simplify the judgment of loops by counting the number of tags you have taken.
Test code
1#include <stdio.h>2 3 intMain ()4 {5 intm, n, tot =-1, I, j, x =-1, y =0;6 inta[ $][ $];7 8scanf"%d%d", &m, &n);9 Ten for(i =0; I < m; i++) One { A for(j =0; J < N; J + +) - { -scanf"%d", &a[i][j]); the } - } - - while(Tot < M *N) + { - while(x +1< m && A[x +1][Y]! =-1) + { Aprintf"%d", a[++X] [y]); atA[x][y] =-1; -tot++; - } - while(Y +1< n && A[x][y +1] != -1) - { -printf"%d", a[x][++y]); inA[x][y] =-1; -tot++; to } + while(X-1>=0&& A[x-1][Y]! =-1) - { theprintf"%d", a[--X] [y]); *A[x][y] =-1; $tot++;Panax Notoginseng } - while(Y-1>=0&& A[x][y-1] != -1) the { +printf"%d", a[x][--y]); AA[x][y] =-1; thetot++; + } - } $ return 0; $}
Base exercise back shape fetch number