爱因斯坦在20世纪初出的这个题目,据说世界上有98%的人答不出来。
在一条街上,有5座房子,喷了5种颜色。每个房里住着不同国籍的人。每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物。
问题是:谁养鱼?
提示:
1、英国人住红色房子
2、瑞典人养狗
3、丹麦人喝茶
4、绿色房子在白色房子左面
5、绿色房子主人喝咖啡
6、抽PallMall香烟的人养鸟
7、黄色房子主人抽Dunhill香烟
8、住在中间房子的人喝牛奶
9、挪威人住第一间房
10、抽Blends香烟的人住在养猫的人隔壁
11、养马的人住抽Dunhill香烟的人隔壁
12、抽BlueMaster的人喝啤酒
13、德国人抽Prince香烟
14、挪威人住蓝色房子隔壁
15、抽Blends香烟的人有一个喝水的邻居
/*
` ````````` `
``````` nspcn.org ````` ```
``
March,2006“““““““““
`` Auto Box Carryer ```` ```
''''' By Linmu ``
`````````` `
先列一张表:
1 2 3 4 5
国家 英 瑞 丹 德 挪
动物 狗 鸟 猫 马 鱼
烟 pall* Du* Blue* Blen* Prince
饮料 茶 咖啡 牛奶 酒 水
颜色 红 绿 白 蓝 黄
*/
include
include
define maxn 6
int color[maxn];
int animal[maxn];
int nationality[maxn];
int drink[maxn];
int smoke[maxn];
int used[maxn][maxn];
int find(int *list,int key){
int i;
for (i=1;i<=5;i++){
if (list==key) return i;
}
return 0;
}
void out(int *list){
int i;
for (i=1;i<=5;i++){
printf(“%d “,list);
}
printf(“\n”);
}
int check(){
// goto loop;
if (drink[3]!=3) return 0;
if (find(drink,1)!=find(nationality,3)) return 0;
if (find(color,2)!=find(drink,2)) return 0;
if (find(smoke,1)!=find(animal,2)) return 0;
if (find(color,5)!=find(smoke,2)) return 0;
if (abs(find(smoke,4)-find(animal,3))!=1) return 0;
if (abs(find(animal,4)-find(smoke,2))!=1) return 0;
if (drink[find(smoke,3)]!=4) return 0;
if (smoke[find(nationality,4)]!=5) return 0;
if (abs(find(smoke,4)-find(drink,5))!=1) return 0;
//loop:
out(color);
out(nationality);
out(animal);
out(drink);
out(smoke);
printf(“\n”);
exit(0);
return 1;
}
void dfs(int step,int *list,int count){
if (count==3 && nationality[1]!=5) return;
if (count==3 && find(color,1)!=find(nationality,1)) return;
if (count==4 && find(nationality,2)!=find(animal,1)) return;
if (count==2 && find(color,2)+1!=find(color,3)) return;
if (count==3 && abs(find(color,4)-find(nationality,5))!=1) return;
int i;
if (step==6){
switch(count){
case 1:dfs(1,nationality,count+1);
case 2:dfs(1,animal,count+1);
case 3:dfs(1,drink,count+1);
case 4:dfs(1,smoke,count+1);
case 5:check();
}
return;
}
for (i=1;i<=5;i++){
if (used[count]) continue;
list[step]=i;
used[count]=1;
dfs(step+1,list,count);
used[count]=0;
}
}
int main(){
freopen(“output.txt”,”w”,stdout);
dfs(1,color,1);
return 0;
}
//
//答案是:
//德国人