爱因斯坦问题
爱因斯坦在20世纪初出的这个谜语,题目是这样的:
1、在一条街上,有5座房子,喷了5种颜色。
2、每个房里住着不同国籍的人。
3、每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物。
提示:
1、英国人住红色房子
2、瑞典人养狗
3、丹麦人喝茶
4、绿色房子在白色房子左面
5、绿色房子主人喝咖啡
6、抽Pall Mall香烟的人养鸟
7、黄色房子主人抽Dunhill香烟
8、住在中间房子的人喝牛奶
9、挪威人住第一间房
10、抽Blends香烟的人住在养猫的人隔壁
11、养马的人住在抽Dunhill香烟的人隔壁
12、抽Blue Master的人喝啤酒
13、德国人抽Prince香烟
14、挪威人住蓝色房子隔壁
15、抽Blends香烟的人有一个喝水的邻居
问题是:谁养鱼?
我写的程序如下,但是运行太慢了,网上由原程序,但是不能在dev-c+下编译。
不知道大家有没有什么好主意???
#include <stdio.h>
enum{english,sweden,danmark,norway,germany}enum_nationality;
enum{red,green,white,yellow,blue}enum_color;
enum{tea,coffee,milk,water,beer}enum_drink;
enum{dog,bird,cat,horse,fish}enum_pet;
enum{pall_mall,dunhill,blends,blue_master,prince}enum_cigratte;
enum{Nationality,Color,Drink,Pet,Cigratte}enum_row;
int a[5][5]={0};
int tmp[5][5]={0};
void print()
{
int j;
for(j=0;j<5;j++)
{
switch(a[0][j])
{
case 0:printf("English");break;
case 1:printf("Sweden");break;
case 2:printf("Danmark");break;
case 3:printf("Norway");break;
case 4:printf("Germany");break;
}
printf("\t");
}
for(j=0;j<5;j++)
{
switch(a[1][j])
{
case 0:printf("Red");break;
case 1:printf("Green");break;
case 2:printf("White");break;
case 3:printf("Yellow");break;
case 4:printf("Blue");break;
}
printf("\t");
}
for(j=0;j<5;j++)
{
switch(a[2][j])
{
case 0:printf("Tea");break;
case 1:printf("Coffee");break;
case 2:printf("Milk");break;
case 3:printf("Water");break;
case 4:printf("Beer");break;
}
printf("\t");
}
for(j=0;j<5;j++)
{
switch(a[3][j])
{
case 0:printf("Dog");break;
case 1:printf("Bird");break;
case 2:printf("Cat");break;
case 3:printf("Horse");break;
case 4:printf("Fish");break;
}
printf("\t");
}
for(j=0;j<5;j++)
{
switch(a[3][j])
{
case 0:printf("Pall Mall");break;
case 1:printf("Dunhill");break;
case 2:printf("Blends");break;
case 3:printf("Blue Master");break;
case 4:printf("Prince");break;
}
printf("\t");
}
printf("\n\n");
}
void cigratte(int count)
{
int j;
if(count==4)
{
for(j=0;j<5;j++)
{
if(tmp[Cigratte][j]==0)
{
a[Cigratte][4]=j;
if(test()==1)
print();
}
}
return ;
}
for(j=0;j<5;j++)
{
if(tmp[4][j]==0)
{
tmp[Cigratte][j]=1;
a[Cigratte][count]=j;
cigratte(count+1);
tmp[Cigratte][j]=0;
}
}
}
void pet(int count)
{
int j;
if(count==4)
{
for(j=0;j<5;j++)
{
if(tmp[Pet][j]==0)
{
a[Pet][4]=j;
cigratte(0);
}
}
return ;
}
for(j=0;j<5;j++)
{
if(tmp[Pet][j]==0)
{
tmp[Pet][j]=1;
a[Pet][count]=j;
pet(count+1);
tmp[Pet][j]=0;
}
}
}
void drink(int count)
{
int j;
if(count==4)
{
for(j=0;j<5;j++)
{
if(tmp[Drink][j]==0)
{
a[Color][4]=j;
pet(0);
}
}
return ;
}
for(j=0;j<5;j++)
{
if(tmp[Color][j]==0)
{
tmp[Color][j]=1;
a[Color][count]=j;
drink(count+1);
tmp[Color][j]=0;
}
}
}
void color(int count)
{
int j;
if(count==4)
{
for(j=0;j<5;j++)
{
if(tmp[Color][j]==0)
{
a[Color][4]=j;
drink(0);
}
}
return ;
}
for(j=0;j<5;j++)
{
if(tmp[Color][j]==0)
{
tmp[Color][j]=1;
a[Color][count]=j;
color(count+1);
tmp[Color][j]=0;
}
}
}
void nationality(int count)
{
int j;
if(count==4)
{
for(j=0;j<5;j++)
{
if(tmp[Nationality][j]==0)
{
a[Nationality][4]=j;
color(0);
}
}
return ;
}
for(j=0;j<5;j++)
{
if(tmp[Nationality][j]==0)
{
tmp[Nationality][j]=1;
a[Nationality][count]=j;
nationality(count+1);
tmp[Nationality][j]=0;
}
}
}
int test()
{
int i,j;
if(a[Drink][2]!=milk)return 0;/*8:住在中间房子的人喝牛奶*/
if(a[Nationality][0]!=norway)return 0;/*9:挪威人住第一间房*/
for(j=0;j<5;j++)
{
if((a[Nationality][j]==english)&&(a[Color][j]!=red)) return 0;/*1:英国人住红色房子*/
if((a[Nationality][j]==sweden)&&(a[Pet][j]!=dog))return 0;/*2:瑞典人养狗*/
if((a[Nationality][j]==danmark)&&(a[Drink][j]!=tea))return 0;/*3:丹麦人喝茶*/
if(a[Color][j]==green)/*4:绿色房子在白色房子左面*/
{
if((j+1)>=5)return 0;
else if(a[Color][j+1]!=white) return 0;
}
if((a[Color][j]==green)&&(a[Drink][j]!=coffee))return 0;/*5:绿色房子主人喝咖啡*/
if((a[Cigratte][j]==pall_mall)&&(a[Pet][j]!=bird))return 0;/*6:抽Pall Mall香烟的人养鸟*/
if((a[Color][j]==yellow)&&(a[Cigratte][j]!=dunhill))return 0;/*7:黄色房子主人抽Dunhill香烟*/
if((a[Cigratte][j]==blue_master)&&(a[Drink][j]!=beer))return 0;/*12:抽Blue Master的人喝啤酒*/
if((a[Nationality][j]==germany)&&(a[Cigratte][j]!=prince))return 0;/*13:德国人抽Prince香烟*/
if(a[Cigratte][j]==blends)/*10:抽Blends香烟的人住在养猫的人隔壁*/
{
if((j==0)&&(a[Pet][j+1]!=cat))return 0;
else if((j==4)&&(a[Pet][j-1]!=cat)) return 0;
else if((a[Pet][j+1]!=cat)||(a[Pet][j-1]!=cat)) return 0;
}
if(a[Pet][j]==horse)/*11:养马的人住在抽Dunhill香烟的人隔壁*/
{
if((j==0)&&(a[Cigratte][j+1]!=dunhill))return 0;
else if((j==4)&&(a[Cigratte][j-1]!=dunhill)) return 0;
else if((a[Cigratte][j+1]!=dunhill)||(a[Cigratte][j-1]!=dunhill)) return 0;
}
if(a[Nationality][j]==norway)/*14:挪威人住蓝色房子隔壁*/
{
if((j==0)&&(a[Color][j+1]!=blue))return 0;
else if((j==4)&&(a[Color][j-1]!=blue)) return 0;
else if((a[Color][j+1]!=blue)||(a[Color][j-1]!=blue)) return 0;
}
if(a[Cigratte][j]==blends)/*15:抽Blends香烟的人有一个喝水的邻居*/
{
if((j==0)&&(a[Drink][j+1]!=water))return 0;
else if((j==4)&&(a[Drink][j-1]!=water)) return 0;
else if((a[Drink][j+1]!=water)||(a[Drink][j-1]!=water)) return 0;
}
}
return 1;
}
int main(int argc, char *argv[])
{
nationality(0);
getchar();
return 0;
}