【NOIP】提高组2015 神奇的幻方
【算法】模拟
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> const int maxn=45; int n,a[maxn][maxn]; int main() { scanf("%d",&n); int x=1,y=n/2+1; a[x][y]=1; for(int i=2;i<=n*n;i++) { if(x==1&&y!=n)x=n,y=y+1;else if(x!=1&&y==n)x=x-1,y=1;else if(x==1&&y==n)x=2,y=n;else if(x!=1&&y!=n) if(a[x-1][y+1])x=x+1; else x=x-1,y=y+1; a[x][y]=i; // printf("[%d]%d %d\n",i,x,y); } for(int i=1;i<=n;i++) { for(int j=1;j<n;j++)printf("%d ",a[i][j]); printf("%d\n",a[i][n]); } return 0; }