您的位置:首页 > 其它

[NOIP2015]神奇的幻方

2016-11-17 08:26 288 查看
直接模拟即可
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int n;
int tot=0;
int s[100][100];
void create()
{
while(tot<n*n)
{++tot;
if(tot==1)s[1][(n/2)+1]=1;
else
{
for(int i=1;i<n;i++)
{
if(s[1][i]&&s[1][i]==tot-1)
{
s
[i+1]=tot;break;continue;
}
}
for(int i=2;i<=n;i++)
{
if(s[i]
&&s[i]
==tot-1)
{
s[i-1][1]=tot;break;continue;
}
}
if(s[1]
==tot-1)
{s[2]
=tot;continue;}
else
{
for(int i=2;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(s[i][j]&&s[i][j]==tot-1)
{
if(!s[i-1][j+1])
{
s[i-1][j+1]=tot;break;break;continue;
}
else s[i+1][j]=tot;break;break;continue;
}
}
}
}}}
return;
}
int main()
{
memset(s,0,sizeof(s));
cin>>n;
create();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<s[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: