Source: Internet
Author: User

22: Magic Square
Total time limit:
Memory Limit:

Magic Square is a very magical n*n matrix, it's each row, each column and the diagonal, plus the number is the same.
We can construct a magic square by the following methods. (Number of orders is odd)
1. The first digit is written in the middle of the first line
2. The next number is written in the upper right of the previous number:
A. If the number is in the first row, the next number is written in the last row, and the number of columns is the right column of the number
B. If the number is in the last column, the next number is written in the first column and the row number is the previous row of the number
C. If the number is in the upper-right corner, or there is a number at the top right of the number, the next digit is written below the number

a number N (n<=20)
magic square of 2n-1 * 2n-1 constructed
by the method
Sample input
Sample output
17 24 1 8 1523 5 7 14 164 6 13 20 2210 12 19 21 311 18 25 2 9
1 /*December 4, 2016 Openjudge daily water problem2 ———— 1.8.22 by Lxzy_zby*/3#include <cstdio>4#include <cstring>5 using namespacestd;6 BOOLb[101][101];7 inta[101][101];8 intMain ()9 {Ten     intn,n=2;//N defines the first assignment Onescanf"%d",&N); A     intx=0, y= (2*N)/2-1;//defining (x, y) initial coordinates -a[0][(2*N)/2-1]=1;//initializes the first row with an intermediate number of 1 -memset (b,0,sizeof(b));//BOOL Array Initialization theb[0][(2*N)/2-1]=1;//Initialize first row middle number bool value is 1 -     intAns= (2*n-1)*(2*n-1)+1, ans1=2*n-2; -      while(1) -     { +         if(n==1) Break;//processing n 1 of data separately -         if(x==0&AMP;&AMP;Y!=ANS1)//a case of +         { Ax=2*n-2, y+=1; ata[x][y]=N; -n++; -b[x][y]=1; -         } -         Else -         { in             if(y==ans1&&x!=0)//B Case -             { tox-=1, y=0; +a[x][y]=N; -n++; theb[x][y]=1;  *             } $             ElsePanax Notoginseng             { -                 if(b[x-1][y+1]==1|| x==0&&y==ans1&&b[0][ans1]==1)//C Case the                 { +X + +; Aa[x][y]=N; then++; +b[x][y]=1; -                 } $                 Else $                 { -                     if(b[x-1][y+1]==0)//Normal situation -                     { thex--; -y++;Wuyia[x][y]=N; theb[x][y]=1; -n++; Wu                     } -                 } About                  $             } -         } -         if(n==ans) -          Break;//if the last assignment has a coordinate value of (2n-1) * (2n-1) After the end of the loop, exit the loop A     } +      for(intI=0; i<=ans1;i++) the      { -           for(intj=0; j<=ans1;j++) $printf"%d", A[i][j]); theprintf"\ n"); the      } the     return 0; the}


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.