4510 Magic square noip2015day1 T1
time limit: 1 sspace limit: 128000 KBtitle level: Golden Gold SolvingView Run ResultsTitle Description
Description
Magic Square is a very magical n∗n matrix: It is made up of numbers, ..., n∗n, and the sum of the numbers on each row, column, and two diagonal lines are the same.
When n is an odd number, we can construct a magic square by the following methods:
First write 1 in the middle of the first line. Then, fill in each number (k= 2,3, ..., n∗n) in the following way from small to large:
1. if (k−1) is in the first row but not in the last column, it will be filled in the right column of the last row, (k−1);
2. if (k−1) in the last column but not in the first row, it will be filled in the first column, (k−1) row of the previous line;
3. if (k−1) is in the last column of the first row, it will be filled directly below (k−1);
4. if (k−1) is neither in the first line nor in the last column, if the upper right of (k−1) is not filled,
Place K in the top right of (k−1), otherwise it will be filled directly below (k−1).
Now given N, please construct n∗n Magic Square according to the above method.
Enter a description
Input Description
The input file has only one row and contains an integer, which is the size of the magic square.
Output description
Output Description
The output file contains n rows, n integers per line, that is, the magic side of the N∗n constructed by the above method. Adjacent two integers are separated by a single space.
Sample input
Sample Input
3
Sample output
Sample Output
8 1 6
3 5 7
4 9 2
Data range and Tips
Data Size & Hint
For 100% of data, 1≤n≤39 and is an odd number.
Category labels
Tags Click here to expandNo label 60 points code (do not know why n>19, can not be processed)
#include <cstdio>#include<iostream>using namespacestd;#defineN 501intN,a[n][n];intDx[n],dy[n];BOOLH[n],l[n];voidDealintk) { if(h[k-1]&&!l[k-1]) {a[n][dy[k-1]+1]=K; DX[K]=N; DY[K]=dy[k-1]+1; if(dx[k]==1) h[k]=1; if(dy[k]==n) l[k]=1; return ; } if(!h[k-1]&&l[k-1]) {a[dx[k-1]-1][1]=K; DX[K]=dx[k-1]-1; DY[K]=1; if(dx[k]==1) h[k]=1; if(dy[k]==n) l[k]=1; return ; } if(h[k-1]&&l[k-1]) {a[dx[k-1]+1][dy[k-1]]=K; DX[K]=dx[k-1]+1; DY[K]=dy[k-1]; if(dx[k]==1) h[k]=1; if(dy[k]==n) l[k]=1; return ; } if(!h[k-1]&&!l[k-1]){ if(!a[dx[k-1]-1][dy[k-1]+1]) {a[dx[k-1]-1][dy[k-1]+1]=K; DX[K]=dx[k-1]-1; DY[K]=dy[k-1]+1; if(dx[k]==1) h[k]=1; if(dy[k]==n) l[k]=1; } Else{a[dx[k-1]+1][dy[k-1]]=K; DX[K]=dx[k-1]+1; DY[K]=dy[k-1]; if(dx[k]==1) h[k]=1; if(dy[k]==n) l[k]=1; } return ; }}intMain () {scanf ("%d",&N); a[1][n/2+1]=1; dx[1]=1; dy[1]= (n+1)/2; h[1]=1; for(intI=2; i<=n*n;i++) {deal (i); } for(intI=1; i<=n;i++){ for(intj=1; j<=n;j++) {printf ("%d", A[i][j]); } Putchar ('\ n'); } return 0;}
Mode of change simulation
AC Code:
#include <iostream>#include<cstdio>using namespacestd;intn,x,y;intmp[ $][ $];intMain () {scanf ("%d",&N); X=1, Y= (n+1)/2; Mp[x][y]=1; for(intI=2; i<=n*n;i++){ if(x==1&&y!=N) x=n,y++; Else if(x!=1&&y==N) x--, y=1; Else if(x==1&&y==N) x++; Else if(!mp[x-1][y+1]) x--, y++; Elsex++; Mp[x][y]=i; } for(intI=1; i<=n;i++){ for(intj=1; j<=n;j++) printf ("%d", Mp[i][j]); printf ("\ n"); } return 0;}
Noip2015day1 T1 4510 Magical Magic Square