COGS 2104. [NOIP2015]神奇的幻方
阿新 • • 發佈:2017-08-26
ima turn upload != return label logs 技術分享 blank
★ 輸入文件:2015magic.in
輸出文件:2015magic.out
簡單對比
時間限制:1 s 內存限制:256 MB
模擬
一開始數組開小了。。
屠龍寶刀點擊就送
#include <cstdio> int n,h[1600],l[1600],hf[45][45]; int main() { freopen("2015magic.in","r",stdin);freopen("2015magic.out","w",stdout); scanf("%d",&n); hf[1][n/2+1]=1; h[1]=1;l[1]=n/2+1; for(int i=2;i<=n*n;++i) { if(h[i-1]==1&&l[i-1]!=n) h[i]=n,l[i]=l[i-1]+1,hf[h[i]][l[i]]=i; else if(h[i-1]!=1&&l[i-1]==n) h[i]=h[i-1]-1,l[i]=1,hf[h[i]][l[i]]=i; else if(h[i-1]==1&&l[i-1]==n) h[i]=h[i-1]+1,l[i]=l[i-1],hf[h[i]][l[i]]=i;else if(h[i-1]!=1&&l[i-1]!=n) { if(!hf[h[i-1]-1][l[i-1]+1]) h[i]=h[i-1]-1,l[i]=l[i-1]+1,hf[h[i]][l[i]]=i; else h[i]=h[i-1]+1,l[i]=l[i-1],hf[h[i]][l[i]]=i; } } for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) j!=n?printf("%d ",hf[i][j]):printf("%d\n",hf[i][j]); return 0; }
COGS 2104. [NOIP2015]神奇的幻方