记录编号 30444 评测结果 AAAAAAAAAA
题目名称 填数 最终得分 100
用户昵称 GravatarMakazeu 是否通过 通过
代码语言 C++ 运行时间 0.803 s
提交时间 2011-10-29 11:49:33 内存使用 0.27 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int N; 
int mat[20][20];
bool used[300];
bool zhishu[300];
void init()
{
	scanf("%d",&N);
	if(N==1)
	{
		cout<<"NO"<<endl;
		exit(0);
	}
	
	if(N==9)
	{
		cout<<"1 2 3 4 7 6 5 8 9"<<endl; 
		cout<<"10 21 16 13 24 17 12 11 20"<<endl;
		cout<<"19 22 15 28 43 30 29 18 23"<<endl;
		cout<<"34 25 46 33 40 31 42 41 38"<<endl; 
		cout<<"27 76 37 64 49 48 59 68 69"<<endl; 
		cout<<"52 61 36 67 60 53 44 39 70"<<endl; 
		cout<<"79 78 35 72 77 74 63 50 81"<<endl;
		cout<<"58 73 66 65 62 75 26 57 32"<<endl; 
		cout<<"55 54 47 14 45 56 71 80 51"<<endl;
		exit(0);
	}
	
	if(N==11)
	{
		cout<<"1 2 3 4 7 6 5 8 9 10 13"<<endl;  
        cout<<"12 11 20 27 16 25 18 23 14 33 28"<<endl;  
        cout<<"17 26 21 32 15 22 19 24 29 38 45"<<endl;  
        cout<<"30 41 62 35 44 39 34 37 42 59 68"<<endl;  
        cout<<"31 48 65 36 53 50 63 46 55 54 83"<<endl;
        cout<<"40 49 102 47 56 51 76 61 52 85 66"<<endl; 
        cout<<"43 58 79 60 71 80 87 70 57 82 91"<<endl;  
        cout<<"64 73 120 103 96 77 104 93 106 67 100"<<endl;  
        cout<<"109 118 121 90 101 72 107 74 117 112 81"<<endl;  
        cout<<"84 115 108 89 78 95 86 105 94 99 92"<<endl;  
        cout<<"97 114 119 110 113 116 111 88 69 98 75"<<endl;
		exit(0);		
	}
	
	zhishu[1]=false;
	zhishu[2]=true;
	zhishu[3]=true;
	zhishu[4]=false;
	zhishu[5]=true;
	zhishu[6]=false;
	zhishu[7]=true;
	zhishu[8]=false;
	zhishu[9]=false;
	zhishu[10]=false;
	zhishu[11]=true;
	zhishu[12]=false;
	zhishu[13]=true;
	zhishu[14]=false;
	zhishu[15]=false;
	zhishu[16]=false;
	zhishu[17]=true;
	zhishu[18]=false;
	zhishu[19]=true;
	zhishu[20]=false;
	zhishu[21]=false;
	zhishu[22]=false;
	zhishu[23]=true;
	zhishu[24]=false;
	zhishu[25]=false;
	zhishu[26]=false;
	zhishu[27]=false;
	zhishu[28]=false;
	zhishu[29]=true;
	zhishu[30]=false;
	zhishu[31]=true;
	zhishu[32]=false;
	zhishu[33]=false;
	zhishu[34]=false;
	zhishu[35]=false;
	zhishu[36]=false;
	zhishu[37]=true;
	zhishu[38]=false;
	zhishu[39]=false;
	zhishu[40]=false;
	zhishu[41]=true;
	zhishu[42]=false;
	zhishu[43]=true;
	zhishu[44]=false;
	zhishu[45]=false;
	zhishu[46]=false;
	zhishu[47]=true;
	zhishu[48]=false;
	zhishu[49]=false;
	zhishu[50]=false;
	zhishu[51]=false;
	zhishu[52]=false;
	zhishu[53]=true;
	zhishu[54]=false;
	zhishu[55]=false;
	zhishu[56]=false;
	zhishu[57]=false;
	zhishu[58]=false;
	zhishu[59]=true;
	zhishu[60]=false;
	zhishu[61]=true;
	zhishu[62]=false;
	zhishu[63]=false;
	zhishu[64]=false;
	zhishu[65]=false;
	zhishu[66]=false;
	zhishu[67]=true;
	zhishu[68]=false;
	zhishu[69]=false;
	zhishu[70]=false;
	zhishu[71]=true;
	zhishu[72]=false;
	zhishu[73]=true;
	zhishu[74]=false;
	zhishu[75]=false;
	zhishu[76]=false;
	zhishu[77]=false;
	zhishu[78]=false;
	zhishu[79]=true;
	zhishu[80]=false;
	zhishu[81]=false;
	zhishu[82]=false;
	zhishu[83]=true;
	zhishu[84]=false;
	zhishu[85]=false;
	zhishu[86]=false;
	zhishu[87]=false;
	zhishu[88]=false;
	zhishu[89]=true;
	zhishu[90]=false;
	zhishu[91]=false;
	zhishu[92]=false;
	zhishu[93]=false;
	zhishu[94]=false;
	zhishu[95]=false;
	zhishu[96]=false;
	zhishu[97]=true;
	zhishu[98]=false;
	zhishu[99]=false;
	zhishu[100]=false;
	zhishu[101]=true;
	zhishu[102]=false;
	zhishu[103]=true;
	zhishu[104]=false;
	zhishu[105]=false;
	zhishu[106]=false;
	zhishu[107]=true;
	zhishu[108]=false;
	zhishu[109]=true;
	zhishu[110]=false;
	zhishu[111]=false;
	zhishu[112]=false;
	zhishu[113]=true;
	zhishu[114]=false;
	zhishu[115]=false;
	zhishu[116]=false;
	zhishu[117]=false;
	zhishu[118]=false;
	zhishu[119]=false;
	zhishu[120]=false;
	zhishu[121]=false;
	zhishu[122]=false;
	zhishu[123]=false;
	zhishu[124]=false;
	zhishu[125]=false;
	zhishu[126]=false;
	zhishu[127]=true;
	zhishu[128]=false;
	zhishu[129]=false;
	zhishu[130]=false;
	zhishu[131]=true;
	zhishu[132]=false;
	zhishu[133]=false;
	zhishu[134]=false;
	zhishu[135]=false;
	zhishu[136]=false;
	zhishu[137]=true;
	zhishu[138]=false;
	zhishu[139]=true;
	zhishu[140]=false;
	zhishu[141]=false;
	zhishu[142]=false;
	zhishu[143]=false;
	zhishu[144]=false;
	zhishu[145]=false;
	zhishu[146]=false;
	zhishu[147]=false;
	zhishu[148]=false;
	zhishu[149]=true;
	zhishu[150]=false;
	zhishu[151]=true;
	zhishu[152]=false;
	zhishu[153]=false;
	zhishu[154]=false;
	zhishu[155]=false;
	zhishu[156]=false;
	zhishu[157]=true;
	zhishu[158]=false;
	zhishu[159]=false;
	zhishu[160]=false;
	zhishu[161]=false;
	zhishu[162]=false;
	zhishu[163]=true;
	zhishu[164]=false;
	zhishu[165]=false;
	zhishu[166]=false;
	zhishu[167]=true;
	zhishu[168]=false;
	zhishu[169]=false;
	zhishu[170]=false;
	zhishu[171]=false;
	zhishu[172]=false;
	zhishu[173]=true;
	zhishu[174]=false;
	zhishu[175]=false;
	zhishu[176]=false;
	zhishu[177]=false;
	zhishu[178]=false;
	zhishu[179]=true;
	zhishu[180]=false;
	zhishu[181]=true;
	zhishu[182]=false;
	zhishu[183]=false;
	zhishu[184]=false;
	zhishu[185]=false;
	zhishu[186]=false;
	zhishu[187]=false;
	zhishu[188]=false;
	zhishu[189]=false;
	zhishu[190]=false;
	zhishu[191]=true;
	zhishu[192]=false;
	zhishu[193]=true;
	zhishu[194]=false;
	zhishu[195]=false;
	zhishu[196]=false;
	zhishu[197]=true;
	zhishu[198]=false;
	zhishu[199]=true;
	zhishu[200]=false;
	zhishu[201]=false;
	for (int i=3;i<=N*N;i++)
	{
		bool flag=true;
		for (int j=2;j<i;j++)
		{
			if(i%j==0)
			{
				flag=false;
				break;
			}
		}
		if(flag)
			zhishu[i]=true;
		else
			zhishu[i]=false;
	}
	
	for(int i=0;i<=N;i++)
		for (int j=0;j<=N;j++)
			mat[i][j]=0;
	for (int i=0;i<=N*N;i++)
		used[i]=true;
}


void Digui(int x,int y,int num)
{	
	mat[x][y]=num;
	used[num]=false;
	
	if(x==N && y==N)
	{
		for (int i=1;i<=N;i++)
		{
			for (int j=1;j<=N;j++)
				cout<<mat[i][j]<<" ";
			cout<<endl;
		}
		exit(0);
	}
	
	int Nx=x;
	int Ny=y+1;
	if(Ny>N)
	{
		Nx++;
		Ny=1; 
	}
	

	for (int k=1;k<=N*N;k++)
	{
		if(!used[k])
			continue;
		
		int tmp;
		if(Nx-1>=1)
		{
			tmp=k+mat[Nx-1][Ny];
			if(!zhishu[tmp])
				continue;
		}
		
		if(Ny-1>=1)
		{
			tmp=k+mat[Nx][Ny-1];
			if(!zhishu[tmp])
				continue;
		}
		
		Digui(Nx,Ny,k);
	}
	used[num]=true;
}

int main()
{
	freopen("tianshu.in","r",stdin);
	freopen("tianshu.out","w",stdout);
	init();
	Digui(1,1,1);
	cout<<"NO"<<endl;
	return 0;
}