Serpentine Fill Array

Source: Internet
Author: User

http://noi.openjudge.cn/ch0108/24/

Total time limit:
1000ms
Memory Limit:
65536kB
Describe

With digital 1,2,3,4,..., n*n This n2 number of snakes filled with the scale of N*n Square.

The Serpentine Fill method is:

For each lower left-right diagonal, numbered from top left to bottom right,..., 2n-1; According to the order from small to large, the numbers from small to large to fill in each slash, where numbered odd from the left to the right to fill, numbered even from the top right to the left to fill.

For example, when n=4, the square is filled with the following form:

1 2 6 73 5 8 134 9 12 1410 11 15 16
Input
Enter a positive integer not greater than 10 N, representing the number of rows of the square.
Output
The matrix is output, with a single space interval between adjacent two elements.
Sample input
4
Sample output
1 2 6 73 5 8 134 9 12 1410 11 15 16

Analysis: This topic should pay attention to look at the input and output cases of the serpentine array of the order, according to the meaning of the topic to operate.

Here the square is divided into the upper left triangle (including the main diagonal) and the lower right triangle two parts are processed separately.

The upper triangular portion of the oblique number equals n, in order to facilitate processing, the oblique sequence K from 0~ (n-1).

The lower triangular portion of the oblique row is n-1, and the oblique sequence k is from 1 to n-1.

On the upper triangle, when K is even, the oblique line goes from small network, and when K is odd, it is the opposite.

However, it is important to note that the direction of the lower triangular diagonal line cannot be determined. When the parity of n is different, the direction of the diagonal line at the end of the upper triangle is not the same, which leads to the different direction of the lower triangle starting diagonal line. Here, a flag variable is used to identify the direction of the lower triangle's starting diagonal line.

1#include <stdio.h>2#include <stdlib.h>3 intMainintargcChar*argv[])4 {5     intn,i,j,t;6     int**A;7     intK;8     intFlag;9     Tenscanf"%d",&n); OneA= (int**)malloc(sizeof(int*)*n); A      for(i=0; i<n;i++) a[i]= (int*)malloc(sizeof(int)*n); -      -t=0; the      for(k=0; k<n;k++)//fills the upper-left triangle (including the main diagonal) -     { -         if((k&1)==0)//odd number of diagonal rows, from bottom left to top right scan fill -         { +I=K; -j=0; +              for(; i>=0; I--, J + +) A             { att++; -a[i][j]=T; -             } -         } -         Else//an even number of diagonal rows, from top right to bottom left scan fill -         { inI=0; -j=K; to              for(; j>=0; i++,j--) +             { -t++; thea[i][j]=T; *             } $         }Panax Notoginseng     } -      the     if((n&1)==0)//flag equals 0 or 1 determines the direction of the first diagonal row in the lower right triangle section +flag=1; A     Elseflag=0; the      for(k=1; k<n;k++)//lower right triangle (excluding main diagonal) +     { -         if(flag==1)//scan fill from bottom left to top right $         { $i=n-1; -j=K; -              for(; j<n;j++,i--) the             { -t++;Wuyia[i][j]=T; the             } -flag=0;  Wu         } -         Else//scan fill from top right to bottom left About         { $I=K; -j=n-1; -              for(; i<n;i++,j--) -             { At++; +a[i][j]=T; the             } -flag=1; $         } the     } the      the      for(i=0; i<n;i++) the     { -          for(j=0; j<n;j++) inprintf"%d", A[i][j]); theprintf"\ n"); the     } About     return 0; the}

In fact, it is possible to control the direction of each diagonal row of the upper and lower triangles entirely by the flag variable. (that is, the diagonal line of the upper triangle is also controlled with flag.) But I was too lazy to rewrite the hehe)

Serpentine Fill Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.