比赛 2025暑期集训第7场 评测结果 WWWWWWWWWW
题目名称 填数 最终得分 0
用户昵称 zz 运行时间 0.049 s
代码语言 C++ 内存使用 3.67 MiB
提交时间 2025-08-11 17:24:30
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
int n;
int ans[11][11];
bool f[121];//想不通 
bool l(int a){
	int sa=sqrt(a);
	for(int i=2;i<=sa;i++){
		if(a%i==0) return 1;
	}
	return 0;
}
void _out(){
    bool b=1;
    for(int l=2;l<=n*n;l++){
        if(!f[l]) b=0;
    }
    if(b){
        for(int k=0;k<n;k++){
            for(int j=0;j<n;j++){
                cout<<ans[k][j]<<" ";
            }
            cout<<endl;
        }
    }else cout<<"NO";
}
void dfs(int c,int k){int i=2;
    bool b=0;
    ans[0][0]=1;
    while(i<=n*n){
    cout<<f[i]<<c<<k<<i<<endl;
        if(!f[i]){
            ans[c][k]=i;
            f[i]=1;
        }
        if(!f[i]){
            i++;
            continue;
        }
        for(int z=0;z<n;z++){
            for(int j=0;j<n;j++){
                cout<<ans[z][j]<<" ";
            }
            cout<<endl;
        }
        cout<<endl;
        ++i;
        if(c>0){
            if(l(ans[c-1][k]+ans[c][k])){
                b=1;
                ans[c][k]=0;
                f[i]=0;
                continue;
            }
        }
        if(k>0){
            if(l(ans[c][k-1]+ans[c][k])){
                b=1;
                ans[c][k]=0;
                f[i]=0;
                continue;
            }
        }
        if(b) i=2;
        if(k!=n-1){dfs(c,k+1);}
        else if(c==n-1){
            _out();
            exit(0);
        }else dfs(c+1,0);
    }
    return;
}
int main(){
    freopen("tianshu.in","r",stdin);
    freopen("tianshu.out","w",stdout);
    cin>>n;
    if(n==1) cout<<1;
    else dfs(0,1);
    return 0;
}