2015 NOIP day1 t1 神奇的幻方 (簡單模擬)
阿新 • • 發佈:2019-02-03
題目連結
題意:略
解答:直接模擬
#include<cstdio>
#include<algorithm>
using namespace std;
#define LL long long
#define pb push_back
#define X first
#define Y second
#define cl(a,b) memset(a,b,sizeof(a))
typedef pair<int,int> P;
const int maxn=100005;
const LL inf=1<<27;
const LL mod=1e9+7 ;
int a[40][40];
int n;
void getXY(int k,int&x,int&y){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[i][j]==k){
x=i;y=j;return ;
}
}
}
}
/**
8 1 6
3 5 7
4 9 2
**/
int main(){
scanf("%d",&n);
a[1][(n+1)/2]=1;
for (int k=2;k<=n*n;k++){
int x,y;
getXY(k-1,x,y);
if(x==1&&y!=n){
a[n][y+1]=k;
}
else if(y==n&&x!=1){
a[x-1][1]=k;
}
else if(x==1&&y==n){
a[x+1][y]=k;
}
else if(x!=1&&y!=n){
if (a[x+-1][y+1]==0){
a[x-1][y+1]=k;
}
else {
a[x+1][y]=k;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
printf("%d%c",a[i][j],j==n?'\n':' ');
}
//printf("\n");
}
return 0;
}