24: Serpentine padding array

Source: Internet
Author: User

24: Serpentine padding array
    • View
    • Submit
    • Statistics
    • Questions
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
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <cstring>5 using namespacestd;6 inttot=1;7 intans=2;8 intnow=1;//1 down 2 up9 inta[101][101];Ten intMain () One { A     intN; -Cin>>N; -     intI=1, j=2; thecout<<1<<" "; -      while(tot!=n*N) -     { -         if(now==1) +         { -              +a[i][j]=ans; Aans++;  attot++; -             if(j==1|| i==N) -             { -                 if(j==1&&i!=N) -i++; -                 ElseJ + +; innow=2; -                 Continue; to             } +             Else -             { thei++; *j--; $             }Panax Notoginseng              -         } the         if(now==2) +         { A              thea[i][j]=ans; +ans++; -tot++; $             if(i==1|| j==N) $             { -                 if(i==1&&j!=N) -J + +; the                 Elsei++; -now=1;Wuyi                 Continue; the             } -             Else Wu             { -i--; AboutJ + +;  $             } -         } -         Elsenow=1;  -     } A      for(intI=1; i<=n;i++) +     { the          for(intj=1; j<=n;j++) -         { $             if(i==1&&j==1)Continue; the             Else thecout<<a[i][j]<<" "; the         } thecout<<Endl; -     } in     return 0; the}

24: Serpentine padding 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.