201412-2 Z-shaped scanning (c language)

Problem Description
  In the image encoding algorithm that need to be given a square matrix of Z-shaped scanning (Zigzag Scan). Given an n × n matrix, Z-shaped scanning process is shown below:

  For the following matrix of 4 × 4,
  . 1. 5. 9. 3
  . 3. 5. 6. 7
  . 9. 4. 4. 6
  . 7. 3. 1. 3
  subjected to Z-shaped scanning after obtaining a sequence of length 16 is:
  1,539,739,547,366,413
  implement a Z-shaped scanning procedure, given a n × n matrix, and outputs the matrix Z-shaped scanning the result of.
Input Format
  Comprising a first line of input integer n, the size of the matrix.
  Enter the second line to the first n + 1 rows each containing n positive integers, separated by spaces, represents a given matrix.
Output Format
  An output line, n × n comprises integers, separated by a space, the matrix shows the results of Z-shaped input after 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 scale cases and agreed with
  1≤n≤500, matrix element is not more than 1000 positive integer.

Ideas:
We are taking the path coordinates written down, and will observe the rule of law will find its horizontal and vertical coordinates, the horizontal and vertical coordinates and the sum is 0 to 2 * n-2, and then we analyzed were 1,2 3 .. .... 2 * n-2 sets these laws, and the time when less than n, when n is equal to and greater than when it is not difficult to find regularity.
 
 1 #include<stdio.h>
 2 int main()
 3 {
 4     int n,i,ii,temp,j;
 5     scanf("%d",&n);
 6     int a[n][n];
 7     for(i=0;i<n;i++)
 8         for(j=0;j<n;j++)
 9             scanf("%d",&a[i][j]);
10         printf("%d ",a[0][0]);
11     for (II = . 1 ; II <* n- 2 ; II ++ )
 12 is      {
 13 is          IF (II < n-)
 14          {
 15              IF (II% 2 == . 1 ) // odd number from small to large is 
16              {
 . 17                  for (I = 0 ; I <II + . 1 ; I ++ )
 18 is                      the printf ( " % D " , A [I] [II- I]);
 . 19              }
 20 is              the else // even number is descending 
21              {
22 is                  for (I = 0 ; I <II + . 1 ; I ++ )
 23 is                      the printf ( " % D " , A [II- I] [I]);
 24              }
 25          }
 26 is          the else 
27          {
 28              IF (II% 2 == . 1 ) // odd number is small to large, the number of outputs is 2 * n-ii times, starting from the abscissa. 1 + n--II 
29              {
 30                  for (I = 0 ; I < 2 * N- . 1 -II; I ++ )
 31                      printf ( "%d ",a[ii-n+1+i][n-1-i]);
32             }
33             else
34             {
35                 for(i=0;i<2*n-1-ii;i++)
36                     printf("%d ",a[n-1-i][ii-n+1+i]);
37             }
38         }
39     }
40 }

 

Guess you like

Origin www.cnblogs.com/zhangtao-o/p/11355061.html