比赛 2025暑期集训第7场 评测结果 ATWAAAAAAA
题目名称 填数 最终得分 80
用户昵称 陆晨洗 运行时间 4.046 s
代码语言 C++ 内存使用 3.65 MiB
提交时间 2025-08-11 15:12:11
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
int n;
bool ans;
int a[50][50]={0};
bool b[200]={0};

bool ss(int x)
{
    int i;
    for(i=2;i<=sqrt(x);i++)
    {
        if(x%i==0)
        {
            return false;
        }
    }
    return true;
}
void mj(int x,int y)
{
    int i,j;
    if(x==n&&y==n+1)
    {
        ans=true;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
        return;
    }
    if(y==n+1)
    {
        y=1;
        x++;
    }
    for(i=2;i<=n*n&&ans==false;i++)
    {
        if(b[i]==false)
        {
            if(a[x-1][y]==0||ss(a[x-1][y]+i)==true)
            {
                if(a[x][y-1]==0||ss(a[x][y-1]+i)==true)
                {
                    b[i]=true;
                    a[x][y]=i;
                    mj(x,y+1);
                    b[i]=false;
                }
            }
        }
    }
}
int main()
{
    freopen("tianshu.in","r",stdin);
    freopen("tianshu.out","w",stdout);
    int i,j;
    cin>>n;
    if(n==1)
    {
        cout<<"NO"<<endl;
        return 0;
    }
    ans=false;b[1]=true;a[1][1]=1;
    mj(1,2);
    if(ans=false)
    {
        cout<<"NO"<<endl;
    }
    return 0;
}