该楼层疑似违规已被系统折叠隐藏此楼查看此楼
//用循环"硬算"4×4魔方阵
#include
intB[]={0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};//1:空闲,0:已用
#defineam[0][0]
#definebm[0][1]
#definecm[0][2]
#definedm[0][3]
#defineem[1][0]
#definefm[1][1]
#definegm[1][2]
#definehm[1][3]
#defineim[2][0]
#definetm[2][1]
#defineum[2][2]
#definevm[2][3]
#definewm[3][0]
#definexm[3][1]
#defineym[3][2]
#definezm[3][3]
main()
{
intm[4][4],j,k;
intOK(int[][4]);
for(a=1;a<=16;a++){B[a]=0;
for(b=1;b<=16;b++)if(B[b]){B[b]=0;
for(c=1;c<=16;c++)if(B[c]){B[c]=0;
for(d=1;d<=16;d++)if(B[d]){B[d]=0;if(a+b+c+d==34)//
for(e=1;e<=16;e++)if(B[e]){B[e]=0;
for(f=1;f<=16;f++)if(B[f]){B[f]=0;
for(g=1;g<=16;g++)if(B[g]){B[g]=0;
for(h=1;h<=16;h++)if(B[h]){B[h]=0;if(e+f+g+h==34)//
for(i=1;i<=16;i++)if(B[i]){B[i]=0;
for(t=1;t<=16;t++)if(B[t]){B[t]=0;
for(u=1;u<=16;u++)if(B[u]){B[u]=0;
for(v=1;v<=16;v++)if(B[v]){B[v]=0;if(i+t+u+v==34)//
for(w=1;w<=16;w++)if(B[w]){B[w]=0;
for(x=1;x<=16;x++)if(B[x]){B[x]=0;
for(y=1;y<=16;y++)if(B[y]){B[y]=0;
for(z=1;z<=16;z++)if(B[z])if(w+x+y+z==34)//
if(OK(m)){printf("\nsolution:\n");
for(j=0;j<4;j++,printf("\n"))
for(k=0;k<4;k++)printf("%3d",m[j][k]);}
B[y]=1;}
B[x]=1;}
B[w]=1;}
B[v]=1;}
B[u]=1;}
B[t]=1;}
B[i]=1;}
B[h]=1;}
B[g]=1;}
B[f]=1;}
B[e]=1;}
B[d]=1;}
B[c]=1;}
B[b]=1;}
B[a]=1;}
}
intOK(intn[][4])
{
intj,k,s;
//for(j=0;j<4;j++){
//for(s=k=0;k<4;k++)
//s+=n[j][k];
//if(s-34)return0;}
for(j=0;j<4;j++){
for(s=k=0;k<4;k++)
s+=n[k][j];
if(s-34)return0;}
if(n[0][0]+n[1][1]+n[2][2]+n[3][3]-34)return0;
return!(n[3][0]+n[2][1]+n[1][2]+n[0][3]-34);
}