题目是:
约束条件:
问题是:谁养鱼?
#include<stdio.h>
char*COL[]={NULL,"红","黄","绿","蓝","白"};
char*PAD[]={NULL,"狗","猫","鱼","鸟","马"};
char*DRK[]={NULL,"茶 ","牛奶","咖啡","啤酒","水 "};
char*GUO[]={NULL,"挪威","英国","德国","丹麦","瑞典"};
char*SMK[]={NULL,"Blends","BlueMaster","Dunhill","Pall Mall","Prince"};
struct{ char guojia, color, pads, drink, smoke; } aa[5];
int OK(void)
{
int i,j;
for(i=0;i<5;i++)
if(aa[i].guojia==2 && aa[i].color==1)goto next1;
return 0;
next1:
for(i=0;i<5;i++)
if(aa[i].guojia==5 && aa[i].pads==1)goto next2;
return 0;
next2:
for(i=0;i<5;i++)
if(aa[i].guojia==4 && aa[i].drink==1)goto next3;
return 0;
next3:
for(i=0;i<5;i++)
if(aa[i].color==3)goto next3_1;
return 0;
next3_1:
for(j=i+1;j<5;j++)
if(aa[j].color==5)goto next4;
return 0;
next4:
for(i=0;i<5;i++)
if(aa[i].color==3 && aa[i].drink==3)goto next5;
return 0;
next5:
for(i=0;i<5;i++)
if(aa[i].smoke==4 && aa[i].pads==4)goto next6;
return 0;
next6:
for(i=0;i<5;i++)
if(aa[i].color==2 && aa[i].smoke==3)goto next7;
return 0;
next7:
for(i=0;i<5;i++)
if(aa[i].guojia==3 && aa[i].smoke==5)goto next10;
return 0;
next10:
for(i=0;i<5;i++)
if(aa[i].smoke==2 && aa[i].drink==4)goto next11;
return 0;
next11:
for(i=0;i<5;i++)
if(aa[i].smoke==3)goto next12_1;
next12_1:
if(i==0){if(aa[1].pads==5)goto next13;}
else if(i==4){if(aa[3].pads==5)goto next13;}
else if(aa[i-1].pads==5||aa[i+1].pads==5)goto next13;
return 0;
next13:
for(i=0;i<5;i++)
if(aa[i].smoke==1)goto next13_1;
next13_1:
if(i==0){if(aa[1].pads==2)goto next14;}
else if(i==4){if(aa[3].pads==2)goto next14;}
else if(aa[i-1].pads==2||aa[i+1].pads==2)goto next14;
return 0;
next14:
for(i=0;i<5;i++)
if(aa[i].smoke==1)goto next14_1;
next14_1:
if(i==0){if(aa[1].drink==5)goto next15;}
else if(i==4){if(aa[3].drink==5)goto next15;}
else if(aa[i-1].drink==5||aa[i+1].drink==5)goto next15;
return 0;
next15:
return 1;
}
char color[4]={1,2,3,5};
char drink[4]={1,3,4,5};
char guojia[4]={2,3,4,5};
char smoke[]={1,2,3,4,5};
char pads[5]={1,2,3,4,5};
int rot(char a[],int n)
{ int i,j,k,t;
for(k=n-1;k>0;k--)
if(a[k-1]<a[k])break;
if(k==0)
{ for(i=0,j=n-1;i<j;i++,j--)
{char t=a[i];
a[i]=a[j];
a[j]=t;
}
return 0;
}
t=a[k-1];i=k;
for(j=k+1;j<n;j++)
if(t<a[j]&&a[j]<a[i])i=j;
a[k-1]=a[i];a[i]=t;
for(i=k;i<n-1;i++)
for(j=k;j<n-1+k-i;j++)
if(a[j]>a[j+1])
{ t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
return 1;
}
int main()
{ int i,j,k,ans=0;
int i1,i2,i3,i4,i5;
aa[1].color=4;
aa[2].drink=2;
aa[0].guojia=1;
for(i1=0;i1<24;i1++){
aa[0].color=color[0];
aa[2].color=color[1];
aa[3].color=color[2];
aa[4].color=color[3];
rot(color,4);
for(i2=0;i2<24;i2++){
aa[0].drink=drink[0];
aa[1].drink=drink[1];
aa[3].drink=drink[2];
aa[4].drink=drink[3];
rot(drink,4);
for(i3=0;i3<24;i3++){
aa[1].guojia=guojia[0];
aa[2].guojia=guojia[1];
aa[3].guojia=guojia[2];
aa[4].guojia=guojia[3];
rot(guojia,4);
for(i4=0;i4<120;i4++){
for(i=0;i<5;i++)
aa[i].smoke=smoke[i];
rot(smoke,5);
for(i5=0;i5<120;i5++){
for(j=0;j<5;j++)
aa[j].pads=pads[j];
rot(pads,5);
if(OK()){
printf("---------------------第%d种解---------------------\n",++ans);
for(k=0;k<5;k++)
printf("第%d户: %s人 %s房子 养%s 喝%s 抽%s\n",k+1,GUO[aa[k].guojia],
COL[aa[k].color],PAD[aa[k].pads],DRK[aa[k].drink],SMK[aa[k].smoke]);
}}}}}}
return 0;
}
运行结果表明:总共有七组可能的解答,具体如下