爱因斯坦谜题:
    在一条街上有颜色互不相同的五栋房子,不同国籍的人分别住在这五栋房子力,每人抽不同品牌的香烟,喝不同的饮料,养不同的宠物。已知如下情况:
1.  英国人住红色房子里。
2.  瑞典人养狗。
3.  丹麦人喝茶。
4.  绿色房子坐落在白色房子的左面。
5.  绿色房子的主人喝咖啡。
6.  抽Pall Mall香烟的人养鸟。
7.  黄色房子的主人抽Dunhill香烟。
8.  挪威人住第一间房子。
9.  五座房子中间的那座的主人喝牛奶。
10. 抽Blends香烟的住在养猫人的隔壁。
11. 养马的人住在抽Dunhill香烟者的隔壁。
12. 抽Blue Master香烟的喝啤酒。
13. 德国人抽Prince香烟。
14. 挪威人住的房子在蓝色房子的隔壁。
15. 抽Blends香烟的人有一个喝水的邻居。
问:谁养鱼?
 
谜题的英文原文:
Let us assume that there are five houses of different colors next to each other on the same road. 
In each house lives a man of a different nationality. Every man has his favorite drink, his
favorite brand of cigarettes, and keeps pets of a particular kind.

1.The Englishman lives in the red house.
2.The Swede keeps dogs.
3.The Dane drinks tea.
4.The green house is just to the left of the white one.
5.The owner of the green house drinks coffee.
6.The Pall Mall smoker keeps birds.
7.The owner of the yellow house smokes Dunhills.
8.The man in the center house drinks milk.
9.The Norwegian lives in the first house.
10.The Blend smoker has a neighbor who keeps cats.
11.The man who smokes Blue Masters drinks bier.
12.The man who keeps horses lives next to the Dunhill smoker.
13.The German smokes Prince.
14.The Norwegian lives next to the blue house.
15.The Blend smoker has a neighbor who drinks water.

The question to be answered is: Who keeps fish?

 这道迷题出自1981年柏林的德国逻辑思考学院。据说世界上只有2%的人能出答案。就连大名鼎鼎的爱因斯坦也成为此题大伤脑筋,所以这道题也经常被国内外知名公司用做面试题目,相信许多朋友都只做出过一个答案,今天就用计算机来看看答案:
 C#代码(代码来源:http://tech.ccidnet.com/art/322/20040304/94061_1.html,在此感谢原作者):
using System;
namespace netsafe.math
{
    
public class ayst
    {
        
/// <summary>
        
/// 问题中的所有元素
        
/// </summary>
        string[,] data = { {"黄房子","蓝房子","白房子","红房子","绿房子"},
                           {
"挪威人","英国人","德国人","丹麦人","瑞典人"},
                           {
"DUNHILL","PRINCE","混合烟""PALL MALL","BLUE MASTER"},
                           {
"咖 啡","矿泉水","","牛奶"," 啤酒 "},
                           {
""," 恐龙","",    "",""}
                         };
        
/// <summary>/// answer用来存放答案
        
/// </summary>
        int[,] answer = new int[66];
        
int[,] ALL = new int[6122];
        
int count = 1;
        
int nLevel = 0;
        
int[] List = new int[6];
        
public static void Main(string[] args)
        {
            ayst c 
= new ayst();
            c.p();    
///生成全排列到all
            c.run();
            Console.Read(); 
/// 按任意键继续
        }
        
void run()
        {
            
int i1, i2, i3, i4, i5;///通过逻辑条件顺序的有效选择来优化程序
            for (i1 = 1; i1 <= 120; i1++)///房子
            {
                
///9 、挪威人住第一间房子
                
///14 、挪威人住在蓝房子旁边
                
///不满足条件就短路
                
///    
                if (ALL[2, i1] != 2continue;
                
for (int j = 0; j < 5; j++, answer[j, 1= ALL[j, i1]) ;
                
for (i2 = 1; i2 <= 120; i2++)///人种
                {
                    
for (int j = 0; j < 5; j++, answer[j, 2= ALL[j, i2]) ;
                    
///9 、挪威人住第一间房子
                    if (ALL[1, i2] != 1continue;
                    
///1、 英国人住在红房子里 
                    
///
                    if (find(14!= find(22)) continue;
                    
///4 、绿房子在白房子左边 
                    
///    
                    if (find(15> find(13)) continue;
                    
for (i3 = 1; i3 <= 120; i3++)///
                    {
                        
for (int j = 0; j < 5; j++, answer[j, 3= ALL[j, i3]) ;
                        
///13、 德国人抽PRINCE烟
                        
///        
                        if (find(23!= find(32)) continue;
                        
///7 、黄房子主人抽DUNHILL烟
                        
///        
                        if (find(11!= find(31)) continue;
                        
for (i4 = 1; i4 <= 120; i4++)///饮料
                        {
                            
for (int j = 0; j < 5; j++, answer[j, 4= ALL[j, i4]) ;
                            
///8 、住在中间那间房子的人喝牛奶    
                            
///        
                            if (ALL[3, i4] != 4continue;
                            
///5 、绿房子主人喝咖啡 
                            
///    
                            if (find(15!= find(41)) continue;
                            
///3 、丹麦人喝茶 
                            
///        
                            if (find(24!= find(43)) continue;
                            
///15 、抽混合烟的人的邻居喝矿泉水    
                            if (Math.Abs(find(33- find(42)) != 1continue;
                            
///12 、抽BLUE    MASTER烟的人喝啤酒
                            
///        
                            if (find(35!= find(45)) continue;
                            
for (i5 = 1; i5 <= 120; i5++)///宠物
                            {
                                
for (int j = 0; j < 5; j++, answer[j, 5= ALL[j, i5]) ;
                                
///10 、抽混合烟的人住在养鱼人的旁边
                                
///    
                                if (Math.Abs(find(33- find(51)) != 1continue;
                                
///2 、瑞典人养了一条狗 
                                
///        
                                if (find(25!= find(55)) continue;
                                
///6 、抽PALL MALL烟的人养了一只鸟    
                                
///        
                                if (find(34!= find(54)) continue;
                                
///11 、养马人住在DUNHILL烟的人旁边 
                                
///    
                                if (Math.Abs(find(53- find(31)) != 1continue;
                                
///
                                
///能活到这里的data,当然是答案喽
                                
///
                                write_answer();
                            }
                        }
                    }
                }
            }
        }
        
/// <summary>
        
/// 非常典型的用递归实现排列组合算法。
        
/// </summary>
        public void p()
        {
            
int nCount, nJudge, key;
            nLevel
++;
            
if (nLevel > 5)
            {
                writeall();
///有一种排列就写到All数组里
                nLevel--;
                
return;
            }
            
for (nCount = 1; nCount <= 5; nCount++)
            {
                key 
= 0;
                
for (nJudge = 0; nJudge <= nLevel - 1; nJudge++)
                    
if (nCount == List[nJudge])
                    {
                        key 
= 1;
                        
break;
                    }
                
if (key == 0)
                {
                    List[nLevel] 
= nCount;
                    p();
                }
            }
            nLevel
--;
        }
        
/// <summary>
        
/// 写入all数组
        
/// </summary>
        void writeall()
        {
            
int i;
            
for (i = 1; i <= 5; i++)
            {
                ALL[i, count] 
= List[i];
            }
            count
++;
        }
        
int find(int i, int j)
        {
            
int k;
            
for (k = 0; k <= 5; k++)
            {
                
if (answer[k, i] == j)
                {
                    
return k;
                }
            }
            
return -1;
        }
        
/// <summary>
        
/// 将答案打印出来
        
/// </summary>
        void write_answer()
        {
            
for (int i = 1; i <= 5; i++)
            {
                
for (int j = 1; j <= 5; j++)
                {
                    Console.Write(data[i 
- 1, answer[j, i] - 1+ ",");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
}

运行以后结果是: 
 
可以看出答案并不唯一,有许多组合理的答案。
还有一种暴力破解的算法
C#代码如下(代码来源:http://www.pconline.com.cn/pcedu/empolder/net/cs/0509/acc/05-09-21-e-2.txt,感谢原作者提供代码让我们学习):
using System;
using System.Collections.Generic;
using System.Text;

namespace 爱因斯坦迷题1
{
    
enum 国籍 { 英国, 瑞典, 丹麦, 挪威, 德国 };
    
enum 颜色 { 红, 绿, 蓝, 黄, 白 };
    
enum 宠物 { 鸟, 猫, 马, 鱼, 狗 };
    
enum 饮料 { 水, 牛奶, 咖啡, 茶, 啤酒 };
    
enum 香烟 { blends, blue, prince, dunhill, pall };

    
public class ProTable
    {
        
private const string rule = @"
            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香烟的人有一个喝水的邻居 
";

        
public string Rule { get { return rule; } }

        
private enum T { 国籍 = 0, 颜色, 宠物, 饮料, 香烟 };
        
private const int N = 5;
        
//求排列
        private static int[,] aid = new int[120, N];
        
static ProTable()
        {
            
int k = 0;
            
for (int i0 = 0; i0 < N; i0++)
            {
                
for (int i1 = 0; i1 < N; i1++)
                {
                    
if (i1 == i0) continue;
                    
for (int i2 = 0; i2 < N; i2++)
                    {
                        
if (i2 == i1 || i2 == i0) continue;
                        
for (int i3 = 0; i3 < N; i3++)
                        {
                            
if (i3 == i2 || i3 == i1 || i3 == i0) continue;
                            
for (int i4 = 0; i4 < N; i4++)
                            {
                                
if (i4 == i3 || i4 == i2 || i4 == i1 || i4 == i0) continue;
                                aid[k, 
0= i0;
                                aid[k, 
1= i1;
                                aid[k, 
2= i2;
                                aid[k, 
3= i3;
                                aid[k, 
4= i4;
                                k
++;
                            }
                        }
                    }
                }
            }
        }


        
//判断矩阵
        
//  国籍,颜色,宠物,饮料,香烟
        
//1
        
//2
        
//3
        
//4
        
//5
        private int[,] array = new int[N, N];

        
//根据排列数组生成
        private void replace(int i, int j)
        {
            
for (int k = 0; k < N; k++)
            {
                array[k, i] 
= aid[j, k];
            }
        }

        
//通过getXX得到相应的行号
        private int get香烟(香烟 n)
        {
            
for (int i = 0; i < array.Length; i++)
                
if (array[i, (int)T.香烟] == (int)n)
                    
return i;
            
return -1;
        }
        
private int get饮料(饮料 n)
        {
            
for (int i = 0; i < array.Length; i++)
                
if (array[i, (int)T.饮料] == (int)n)
                    
return i;
            
return -1;
        }
        
private int get宠物(宠物 n)
        {
            
for (int i = 0; i < array.Length; i++)
                
if (array[i, (int)T.宠物] == (int)n)
                    
return i;
            
return -1;
        }
        
private int get国籍(国籍 n)
        {
            
for (int i = 0; i < array.Length; i++)
                
if (array[i, (int)T.国籍] == (int)n)
                    
return i;
            
return -1;
        }
        
private int get颜色(颜色 n)
        {
            
for (int i = 0; i < array.Length; i++)
                
if (array[i, (int)T.颜色] == (int)n)
                    
return i;
            
return -1;
        }


        
//规则:
        
//1、英国人住红色房子
        
//2、瑞典人养狗
        
//3、丹麦人喝茶
        
//4、绿色房子在白色房子左面
        
//5、绿色房子主人喝咖啡
        
//6、抽Pall Mall 香烟的人养鸟
        
//7、黄色房子主人抽Dunhill 香烟
        
//8、住在中间房子的人喝牛奶
        
//9、 挪威人住第一间房
        
//10、抽Blends香烟的人住在养猫的人隔壁
        
//11、养马的人住抽Dunhill 香烟的人隔壁
        
//12、抽Blue Master的人喝啤酒
        
//13、德国人抽Prince香烟
        
//14、挪威人住蓝色房子隔壁
        
//15、抽Blends香烟的人有一个喝水的邻居


        
//1、英国人住红色房子
        private bool assert1()
        {
            
if (!(
                array[get国籍(国籍.英国), (
int)T.颜色] == (int)颜色.红
            ))
                
return false;
            
return true;
        }
        
//2、瑞典人养狗
        private bool assert2()
        {
            
if (!(
                array[get国籍(国籍.瑞典), (
int)T.宠物] == (int)宠物.狗
            ))
                
return false;

            
return true;
        }
        
//3、丹麦人喝茶
        private bool assert3()
        {
            
if (!(
               array[get国籍(国籍.丹麦), (
int)T.饮料] == (int)饮料.茶
            ))
                
return false;

            
return true;
        }
        
//4、绿色房子在白色房子左面
        private bool assert4()
        {
            
if (!(
                   get颜色(颜色.绿) 
== (get颜色(颜色.白) - 1//另一种理解get颜色(颜色.绿) < get颜色(颜色.白)
                ))
                
return false;

            
return true;
        }
        
//5、绿色房子主人喝咖啡
        private bool assert5()
        {
            
if (!(
                array[get颜色(颜色.绿), (
int)T.饮料] == (int)饮料.咖啡
             ))
                
return false;

            
return true;
        }
        
//6、抽Pall Mall 香烟的人养鸟
        private bool assert6()
        {
            
if (!(
                array[get香烟(香烟.pall), (
int)T.宠物] == (int)宠物.鸟
            ))
                
return false;

            
return true;
        }
        
//7、黄色房子主人抽Dunhill 香烟
        private bool assert7()
        {
            
if (!(
               array[get颜色(颜色.黄), (
int)T.香烟] == (int)香烟.dunhill
            ))
                
return false;

            
return true;
        }
        
//8、住在中间房子的人喝牛奶
        private bool assert8()
        {
            
if (!(
              array[
2, (int)T.饮料] == (int)饮料.牛奶
            ))
                
return false;

            
return true;
        }
        
//9、 挪威人住第一间房
        private bool assert9()
        {
            
int i = get国籍(国籍.挪威);
            
if (!(
               i 
== 0 || i == 4
            ))
                
return false;

            
return true;
        }
        
//10、抽Blends香烟的人住在养猫的人隔壁
        private bool assert10()
        {
            
int t1 = get香烟(香烟.blends), t2 = get宠物(宠物.猫);
            
if (!(
              t1 
== (t2 + 1|| t1 == (t2 - 1)
            ))
                
return false;

            
return true;
        }
        
//11、养马的人住抽Dunhill 香烟的人隔壁
        private bool assert11()
        {
            
int t1 = get宠物(宠物.马);
            
int t2 = get香烟(香烟.dunhill);
            
if (!(
                t1 
== (t2 + 1|| t1 == (t2 - 1)
            ))
                
return false;
            
return true;
        }
        
//12、抽Blue Master的人喝啤酒
        private bool assert12()
        {
            
if (!(
                array[get香烟(香烟.blue), (
int)T.饮料] == (int)饮料.啤酒
            ))
                
return false;

            
return true;
        }
        
//13、德国人抽Prince香烟
        private bool assert13()
        {
            
if (!(
               array[get国籍(国籍.德国), (
int)T.香烟] == (int)香烟.prince
           ))
                
return false;

            
return true;
        }
        
//14、挪威人住蓝色房子隔壁
        private bool assert14()
        {
            
int t1 = get国籍(国籍.挪威);
            
int t2 = get颜色(颜色.蓝);
            
if (!(
                t1 
== (t2 + 1|| t1 == (t2 - 1)
            ))
                
return false;

            
return true;
        }
        
//15、抽Blends香烟的人有一个喝水的邻居
        private bool assert15()
        {
            
int t1 = get香烟(香烟.blends);
            
int t2 = get饮料(饮料.水);
            
if (!(
                t1 
== (t2 + 1|| t1 == (t2 - 1)
            ))
                
return false;
            
return true;
        }

        
private bool assert()
        {
            
return assert1() && assert2() && assert3() && assert4() && assert5() && assert6() && assert7() && assert8() && assert9() &&
                assert10() 
&& assert11() && assert12() && assert13() && assert14() && assert15();
        }
        
/*纯暴力算法以作比较
        public void Solve_()
        {
            for (int i0 = 0; i0 < aid.GetUpperBound(0); i0++)
            {
                replace(0, i0);
                for (int i1 = 0; i1 < aid.GetUpperBound(0); i1++)
                {
                    replace(1, i1);
                    for (int i2 = 0; i2 < aid.GetUpperBound(0); i2++)
                    {
                        replace(2, i2);
                        for (int i3 = 0; i3 < aid.GetUpperBound(0); i3++)
                        {
                            replace(3, i3);
                            for (int i4 = 0; i4 < aid.GetUpperBound(0); i4++)
                            {
                                replace(4, i4);
                                if (assert())
                                {
                                    Console.WriteLine(this);
                                }
                            }
                        }
                    }
                }
            }
        }
        
*/
        
public void Solve()
        {
            
//解号
            int sn = 1;

            
//逐步生成判别表的算法
            for (int i0 = 0; i0 < aid.GetUpperBound(0); i0++)
            {
                replace((
int)T.国籍, i0);
                
if (!assert9())
                    
continue;
                
for (int i1 = 0; i1 < aid.GetUpperBound(0); i1++)
                {
                    replace((
int)T.饮料, i1);
                    
if (!assert8())
                        
continue;
                    
if (!(assert3()))
                        
continue;

                    
for (int i2 = 0; i2 < aid.GetUpperBound(0); i2++)
                    {
                        replace((
int)T.颜色, i2);
                        
if (!assert4())
                            
continue;
                        
if (!(assert1() && assert14() && assert5()))
                            
continue;
                        
for (int i3 = 0; i3 < aid.GetUpperBound(0); i3++)
                        {
                            replace((
int)T.宠物, i3);
                            
if (!(assert2()))
                                
continue;
                            
for (int i4 = 0; i4 < aid.GetUpperBound(0); i4++)
                            {
                                replace((
int)T.香烟, i4);
                                
if (!(assert6() && assert7() && assert10() && assert11() && assert12() && assert15() && assert13()))
                                    
continue;
                                
if (assert())
                                {
                                    Console.WriteLine(
"解:" + sn++);
                                    Console.WriteLine(
this);
                                }
                            }
                        }
                    }
                }
            }
        }
        
//国籍=0,颜色,宠物,饮料,香烟
        public override string ToString()
        {
            StringBuilder sb 
= new StringBuilder();
            
for (int i = 0; i < 5; i++)
            {
                sb.Append((i 
+ 1).ToString() + ":  ");
                sb.Append(Enum.GetName(
typeof(国籍), array[i, (int)T.国籍]) + ",  ");
                sb.Append(Enum.GetName(
typeof(颜色), array[i, (int)T.颜色]) + ",  ");
                sb.Append(Enum.GetName(
typeof(宠物), array[i, (int)T.宠物]) + ",  ");
                sb.Append(Enum.GetName(
typeof(饮料), array[i, (int)T.饮料]) + ",  ");
                sb.Append(Enum.GetName(
typeof(香烟), array[i, (int)T.香烟]) + "\n");
            }
            
return sb.ToString();
        }
    }

    
class Program
    {
        
static void Main(string[] args)
        {
            ProTable t 
= new ProTable();
            t.Solve(); 
        }
    }
}

运行的结果是:

这个结果和第一种算法的结果有点出入,只有两种答案,但仔细看,这两组答案是完全相同的,等于只有唯一的一种答案,这种解答的方法,算法还需要研究学习的.

另外也有人用java实现了该算法,收录的代码如下(代码出处:http://www.matrix.org.cn/resource/article/2005-11-10/java_43941.html 同样在此感谢原作者):
/*
 * Created on 2005-11-8
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 
*/
package com.netengine.test;

/**
 * 
@author cds
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 
*/
public class WhoFeedsFish {
        
        
private static final String problem = "爱因斯坦的推理题:\n\n"+
                
"1.有5栋5种颜色的房子\n"+
                
"2.每一位房子的主人国籍都不同\n"+
                
"3.这五个人每人只喝一个牌子的饮料,只抽一个牌子的香烟,只养一种宠物\n"+
                
"4.没有人有相同的宠物,抽相同牌子的烟,喝相同牌子的饮料\n\n"+
                
"已知条件:\n"+
                
"1.英国人住在红房子里\n"+
                
"2.瑞典人养了一条狗\n"+
                
"3.丹麦人喝茶\n"+
                
"4.绿房子在白房子的左边\n"+
                
"5.绿房子主人喝咖啡\n"+
                
"6.抽pallmall烟的人养了一只鸟\n"+
                
"7.黄房子主人抽dunhill烟\n"+
                
"8.住在中间房子的人喝牛奶\n"+
                
"9.挪威人住在第一间房子\n"+
                
"10.抽混合烟的人住在养猫人的旁边\n"+
                
"11.养马人住在抽dunhill烟人的旁边\n"+
                
"12.抽bluemaster烟的人喝啤酒\n"+
                
"13.德国人抽prince烟\n"+
                
"14.挪威人住在蓝房子旁边\n"+
                
"15.抽混合烟的人的邻居喝矿泉水\n\n"+
                
"问题:谁养鱼?\n";
        
        
/**
         * 
@return Returns the problem.
         
*/
        
public String getProblem() {
                
return problem;
        }
        
        
private static final int NATIONALITY_ENGLISH = 1;
        
private static final int NATIONALITY_SWIDISH = 2;
        
private static final int NATIONALITY_DAMARK  = 3;
        
private static final int NATIONALITY_NORWAY  = 4;
        
private static final int NATIONALITY_GERMAN  = 5;
        
private int[] nationalities = new int[5];
        
    
private static final int COLOR_RED    = 1;
        
private static final int COLOR_GREEN  = 2;
        
private static final int COLOR_YELLOW = 3;
        
private static final int COLOR_WHITE  = 4;
        
private static final int COLOR_BLUE   = 5;
        
private int[] colors = new int[5];
        
        
private static final int PET_DOG   = 1;
        
private static final int PET_BIRD  = 2;
        
private static final int PET_CAT   = 3;
        
private static final int PET_HORSE = 4;
        
private static final int PET_FISH  = 5;
        
private int[] pets = new int[5];
        
        
private static final int DRINK_TEA    = 1;
        
private static final int DRINK_COFFEE = 2;
        
private static final int DRINK_MILK   = 3;
        
private static final int DRINK_BEER   = 4;
        
private static final int DRINK_WATER  = 5;
        
private int[] drinks = new int[5];
        
        
private static final int TOBACCO_PALLMALL   = 1;
        
private static final int TOBACCO_DUNHILL    = 2;
        
private static final int TOBACCO_BLUEMASTER = 3;
        
private static final int TOBACCO_PRINCE     = 4;
        
private static final int TOBACCO_MIXED      = 5;
        
private int[] tobaccoes = new int[5];
        
        
//5*5的二维数组,答案就在其中:
        private int[][] key ={nationalities,colors,pets,drinks,tobaccoes}; 
        
        
private static final int[][] values = {
                        {
1,2,3,4,5},
                        {
1,2,3,5,4},
                        {
1,2,4,3,5},
                        {
1,2,4,5,3},
                        {
1,2,5,3,4},
                        {
1,2,5,4,3},
                        {
1,3,2,4,5},
                        {
1,3,2,5,4},
                        {
1,3,4,2,5},
                        {
1,3,4,5,2},
                        {
1,3,5,2,4},
                        {
1,3,5,4,2},
                        {
1,4,2,3,5},
                        {
1,4,2,5,3},
                        {
1,4,3,2,5},
                        {
1,4,3,5,2},
                        {
1,4,5,2,3},
                        {
1,4,5,3,2},
                        {
1,5,2,3,4},
                        {
1,5,2,4,3},
                        {
1,5,3,2,4},
                        {
1,5,3,4,2},
                        {
1,5,4,2,3},
                        {
1,5,4,3,2},
                        {
2,1,3,4,5},
                        {
2,1,3,5,4},
                        {
2,1,4,3,5},
                        {
2,1,4,5,3},
                        {
2,1,5,3,4},
                        {
2,1,5,4,3},
                        {
2,3,1,4,5},
                        {
2,3,1,5,4},
                        {
2,3,4,1,5},
                        {
2,3,4,5,1},
                        {
2,3,5,1,4},
                        {
2,3,5,4,1},
                        {
2,4,1,3,5},
                        {
2,4,1,5,3},
                        {
2,4,3,1,5},
                        {
2,4,3,5,1},
                        {
2,4,5,1,3},
                        {
2,4,5,3,1},
                        {
2,5,1,3,4},
                        {
2,5,1,4,3},
                        {
2,5,3,1,4},
                        {
2,5,3,4,1},
                        {
2,5,4,1,3},
                        {
2,5,4,3,1},
                        {
3,1,2,4,5},
                        {
3,1,2,5,4},
                        {
3,1,4,2,5},
                        {
3,1,4,5,2},
                        {
3,1,5,2,4},
                        {
3,1,5,4,2},
                        {
3,2,1,4,5},
                        {
3,2,1,5,4},
                        {
3,2,4,1,5},
                        {
3,2,4,5,1},
                        {
3,2,5,1,4},
                        {
3,2,5,4,1},
                        {
3,4,1,2,5},
                        {
3,4,1,5,2},
                        {
3,4,2,1,5},
                        {
3,4,2,5,1},
                        {
3,4,5,1,2},
                        {
3,4,5,2,1},
                        {
3,5,1,2,4},
                        {
3,5,1,4,2},
                        {
3,5,2,1,4},
                        {
3,5,2,4,1},
                        {
3,5,4,1,2},
                        {
3,5,4,2,1},
                        {
4,1,2,3,5},
                        {
4,1,2,5,3},
                        {
4,1,3,2,5},
                        {
4,1,3,5,2},
                        {
4,1,5,2,3},
                        {
4,1,5,3,2},
                        {
4,2,1,3,5},
                        {
4,2,1,5,3},
                        {
4,2,3,1,5},
                        {
4,2,3,5,1},
                        {
4,2,5,1,3},
                        {
4,2,5,3,1},
                        {
4,3,1,2,5},
                        {
4,3,1,5,2},
                        {
4,3,2,1,5},
                        {
4,3,2,5,1},
                        {
4,3,5,1,2},
                        {
4,3,5,2,1},
                        {
4,5,1,2,3},
                        {
4,5,1,3,2},
                        {
4,5,2,1,3},
                        {
4,5,2,3,1},
                        {
4,5,3,1,2},
                        {
4,5,3,2,1},
                        {
5,1,2,3,4},
                        {
5,1,2,4,3},
                        {
5,1,3,2,4},
                        {
5,1,3,4,2},
                        {
5,1,4,2,3},
                        {
5,1,4,3,2},
                        {
5,2,1,3,4},
                        {
5,2,1,4,3},
                        {
5,2,3,1,4},
                        {
5,2,3,4,1},
                        {
5,2,4,1,3},
                        {
5,2,4,3,1},
                        {
5,3,1,2,4},
                        {
5,3,1,4,2},
                        {
5,3,2,1,4},
                        {
5,3,2,4,1},
                        {
5,3,4,1,2},
                        {
5,3,4,2,1},
                        {
5,4,1,2,3},
                        {
5,4,1,3,2},
                        {
5,4,2,1,3},
                        {
5,4,2,3,1},
                        {
5,4,3,1,2},
                        {
5,4,3,2,1}
        };
        
        
public void printKey(){
                
for(int i = 0; i < 5; i ++){
                        print(
"nationality",key[0][i]);
                }
                System.out.println();
                
for(int i = 0; i < 5; i ++){
                        print(
"color",key[1][i]);
                }
                System.out.println();
                
for(int i = 0; i < 5; i ++){
                        print(
"pet",key[2][i]);
                }
                System.out.println();
                
for(int i = 0; i < 5; i ++){
                        print(
"drink",key[3][i]);
                }
                System.out.println();
                
for(int i = 0; i < 5; i ++){
                        print(
"tobacco",key[4][i]);
                }
                System.out.println();
                
        }
        
        
private void print(String item,int index){
                
if(false){
            }
else if("nationality".equals(item)){
                    
switch(index){
                            
case 1:
                                    System.out.print(
"英国人\t");
                                    
break;
                            
case 2:
                                    System.out.print(
"瑞典人\t");
                                    
break;
                            
case 3:
                                    System.out.print(
"丹麦人\t");
                                    
break;
                            
case 4:
                                    System.out.print(
"挪威人\t");
                                    
break;
                            
case 5:
                                    System.out.print(
"德国人\t");
                                    
break;
                    }
            }
else if("color".equals(item)){
                    
switch(index){
                    
case 1:
                            System.out.print(
"红房子\t");
                            
break;
                    
case 2:
                            System.out.print(
"绿房子\t");
                            
break;
                    
case 3:
                            System.out.print(
"黄房子\t");
                            
break;
                    
case 4:
                            System.out.print(
"白房子\t");
                            
break;
                    
case 5:
                            System.out.print(
"蓝房子\t");
                            
break;
               }
        }
else if("pet".equals(item)){
                    
switch(index){
                    
case 1:
                            System.out.print(
"狗\t");
                            
break;
                    
case 2:
                            System.out.print(
"鸟\t");
                            
break;
                    
case 3:
                            System.out.print(
"猫\t");
                            
break;
                    
case 4:
                            System.out.print(
"马\t");
                            
break;
                    
case 5:
                            System.out.print(
"鱼\t");
                            
break;
               }
        }
else if("drink".equals(item)){
                    
switch(index){
                    
case 1:
                            System.out.print(
"茶\t");
                            
break;
                    
case 2:
                            System.out.print(
"咖啡\t");
                            
break;
                    
case 3:
                            System.out.print(
"牛奶\t");
                            
break;
                    
case 4:
                            System.out.print(
"啤酒\t");
                            
break;
                    
case 5:
                            System.out.print(
"水\t");
                            
break;
               }
        }
else if("tobacco".equals(item)){
                    
switch(index){
                    
case 1:
                            System.out.print(
"PALLMALL\t");
                            
break;
                    
case 2:
                            System.out.print(
"DUNHILL\t");
                            
break;
                    
case 3:
                            System.out.print(
"BLUEMASTER\t");
                            
break;
                    
case 4:
                            System.out.print(
"PRINCE\t");
                            
break;
                    
case 5:
                            System.out.print(
"混合烟\t");
                            
break;
               }
        }
        }

//        条件1:英国人住在红房子里  01
        private boolean check01(){
                
for(int i = 0; i < nationalities.length; i ++){
                        
if(key[0][i] == NATIONALITY_ENGLISH){
                                
if(key[1][i] != COLOR_RED){
                                        
return false;
                                }
                                
else{
                                        
return true;
                                }
                        }
                }
                
return false;
        }
        
//        条件2:瑞典人养了一条狗  02
        private boolean check02(){
                
for(int i = 0; i < nationalities.length; i ++){
                        
if(key[0][i] == NATIONALITY_SWIDISH){
                                
if(key[2][i] != PET_DOG){
                                        
return false;
                                }
                                
else{
                                        
return true;
                                }
                        }
                }
                
return false;
        }

//        条件4:绿房子在白房子的左边  1
        private boolean check1(){
                
for(int i = 0; i < colors.length; i ++){
                        
if(key[1][i] == COLOR_GREEN){
                                
for(int j = 0; j < colors.length; j ++){
                                        
if(key[1][j] == COLOR_WHITE){
                                                
if(i > j){
                                                        
return false;
                                                }
                                                
else{
                                                        
return true;
                                                }
                                        }
                                }
                        }
                }
                
return false;
        }
        
//        条件8:住在中间房子的人喝牛奶  3        
        private boolean check3(){        
                
return key[3][2== DRINK_MILK?true:false;
        }
        
//        条件9:挪威人住在第一间房子  0
        private boolean check0(){
                
if(key[0][0!= NATIONALITY_NORWAY){
                        
return false;
                }
                
return true;
        }
        
//        14.挪威人住在蓝房子旁边  01
        private boolean check011(){
                
for(int i = 0; i < nationalities.length; i ++){
                        
if(key[0][i] == NATIONALITY_NORWAY){
                                
for(int j = 0; j < colors.length; j ++){
                                        
if(key[1][j] == COLOR_BLUE){
                                                
if(Math.abs(i - j) == 1){
                                                        
return true;
                                                }
else{
                                                        
return false;
                                                }
                                        }
                                }
                        }
                }                
                
return false;
        }
        

        
///////////////////////////////////////////////////////////////////
        
        
//条件1,2,3,4,8,9,14被拿出来提前检测(预检测以去除不必要的操作,减少执行时间)
        private boolean check(){

//                条件3:丹麦人喝茶  03
                for(int i = 0; i < nationalities.length; i ++){
                        
if(key[0][i] == NATIONALITY_DAMARK){
                                
if(key[3][i] != DRINK_TEA){
                                        
return false;
                                }
                                
else{
                                        
break;
                                }
                        }
                }

//                条件5:绿房子主人喝咖啡  13
                for(int i = 0; i < colors.length; i ++){
                        
if(key[1][i] == COLOR_GREEN){
                                
if(key[3][i] != DRINK_COFFEE){
                                        
return false;
                                }
                                
else{
                                        
break;
                                }
                        }
                }
                
//                条件6:抽pallmall烟的人养了一只鸟  24
                for(int i = 0; i < tobaccoes.length; i ++){
                        
if(key[4][i] == TOBACCO_PALLMALL){
                                
if(key[2][i] != PET_BIRD){
                                        
return false;
                                }
                                
else{
                                        
break;
                                }
                        }
                }
                
//                条件7:黄房子主人抽dunhill烟  14
                for(int i = 0; i < colors.length; i ++){
                        
if(key[1][i] == COLOR_YELLOW){
                                
if(key[4][i] != TOBACCO_DUNHILL){
                                        
return false;
                                }
                                
else{
                                        
break;
                                }
                        }
                }

//                条件10:抽混合烟的人住在养猫人的旁边  24
                for(int i = 0; i < tobaccoes.length; i ++){
                        
if(key[4][i] == TOBACCO_MIXED){
                                
for(int j = 0; j < pets.length; j ++){
                                        
if(key[2][j] == PET_CAT){
                                                
if(i - j != 1 && i - j != -1){
                                                        
return false;
                                                }
                                                
break;
                                        }
                                }
                                
break;
                        }
                }        
                
//                条件11:养马人住在抽dunhill烟人的旁边  24
                for(int i = 0; i < pets.length; i ++){
                        
if(key[2][i] == PET_HORSE){
                                
for(int j = 0; j < tobaccoes.length; j ++){
                                        
if(key[4][j] == TOBACCO_DUNHILL){
                                                
if(i - j != 1 && i - j != -1){
                                                        
return false;
                                                }
                                                
break;
                                        }
                                }
                                
break;
                        }
                }        
                
//                条件12:抽bluemaster烟的人喝啤酒  34
                for(int i = 0; i < tobaccoes.length; i ++){
                        
if(key[4][i] == TOBACCO_BLUEMASTER){
                                
if(key[3][i] != DRINK_BEER){
                                        
return false;
                                }
                                
else{
                                        
break;
                                }
                        }
                }
                
//                13.德国人抽prince烟  04
                for(int i = 0; i < nationalities.length; i ++){
                        
if(key[0][i] == NATIONALITY_GERMAN){
                                
if(key[4][i] != TOBACCO_PRINCE){
                                        
return false;
                                }
                                
else{
                                        
break;
                                }
                        }
                }

//                15.抽混合烟的人的邻居喝矿泉水  34
                for(int i = 0; i < tobaccoes.length; i ++){
                        
if(key[4][i] == TOBACCO_MIXED){
                                
for(int j = 0; j < drinks.length; j ++){
                                        
if(key[3][j] == DRINK_WATER){
                                                
if((i - j != 1&& (i - j != -1)){
                                                        
return false;
                                                }
                                                
else{
                                                        
break;
                                                }
                                        }
                                }
                                
break;
                        }
                }
                
//满足所有条件:
                return true;
        }
        
        
public void run(){
                
int count = 0;
                
for(int a = 0; a < 120; a ++){
                        
for(int i = 0; i < 5; i ++){
                                key[
0][i] = values[a][i];
                        }
                        
if(!check0()){continue;}                        
                        
                        
for(int b = 0; b < 120; b ++){
                                
for(int i = 0; i < 5; i ++){
                                        key[
1][i] = values[b][i];
                                }
                                
if(!check01() || !check011() || !check1()){continue;}
                                
                                
for(int c = 0; c < 120; c ++){
                                        
for(int i = 0; i < 5; i ++){
                                                key[
2][i] = values[c][i];
                                        }
                                        
if(!check02()){continue;}
                                        
                                        
for(int d = 0; d < 120; d ++){
                                                
for(int i = 0; i < 5; i ++){
                                                        key[
3][i] = values[d][i];
                                                }
                                                
if(!check3()){continue;}
                                                
                                                
for(int e = 0; e < 120; e ++){
                                                        
for(int i = 0; i < 5; i ++){
                                                                key[
4][i] = values[e][i];
                                                        }
                                                        
if(!check()){continue;}
                                                        System.out.println(
"答案" + (++count));
                                                        printKey();
                                                        System.out.println(
"-----------------------"+
                                                                     
"-----------------------");
                                                        
                                                }
                                                
                                        }
                                        
                                }
                                
                        }
                }
        }
        
        
public static void main(String[] args){
                WhoFeedsFish wff 
= new WhoFeedsFish();
                System.out.println(wff.getProblem());
                System.out.println(
"========================Start========================");
                wff.run();
                System.out.println(
"========================End========================");
        }

}

运行后同样也可以得到7组答案:
================================Start=================================
答案1
挪威人        丹麦人        英国人        德国人        瑞典人        
黄房子        蓝房子        红房子        绿房子        白房子        
猫        马        鸟        鱼        狗        
水        茶        牛奶        咖啡        啤酒        
DUNHILL        混合烟        PALLMALL        PRINCE        BLUEMASTER        
----------------------------------------------------------------------
答案2
挪威人        德国人        英国人        丹麦人        瑞典人        
绿房子        蓝房子        红房子        黄房子        白房子        
鸟        猫        马        鱼        狗        
咖啡        水        牛奶        茶        啤酒        
PALLMALL        PRINCE        混合烟        DUNHILL        BLUEMASTER        
----------------------------------------------------------------------
答案3
挪威人        德国人        英国人        丹麦人        瑞典人        
绿房子        蓝房子        红房子        黄房子        白房子        
鸟        鱼        马        猫        狗        
咖啡        水        牛奶        茶        啤酒        
PALLMALL        PRINCE        混合烟        DUNHILL        BLUEMASTER        
----------------------------------------------------------------------
答案4
挪威人        德国人        瑞典人        英国人        丹麦人        
绿房子        蓝房子        黄房子        红房子        白房子        
鱼        猫        狗        马        鸟        
咖啡        水        牛奶        啤酒        茶        
混合烟        PRINCE        DUNHILL        BLUEMASTER        PALLMALL        
----------------------------------------------------------------------
答案5
挪威人        德国人        瑞典人        英国人        丹麦人        
绿房子        蓝房子        白房子        红房子        黄房子        
鸟        猫        狗        马        鱼        
咖啡        水        牛奶        啤酒        茶        
PALLMALL        PRINCE        混合烟        BLUEMASTER        DUNHILL        
----------------------------------------------------------------------
答案6
挪威人        德国人        瑞典人        丹麦人        英国人        
绿房子        蓝房子        白房子        黄房子        红房子        
鸟        猫        狗        鱼        马        
咖啡        水        牛奶        茶        啤酒        
PALLMALL        PRINCE        混合烟        DUNHILL        BLUEMASTER        
----------------------------------------------------------------------
答案7
挪威人        德国人        瑞典人        丹麦人        英国人        
绿房子        蓝房子        白房子        黄房子        红房子        
鸟        鱼        狗        猫        马        
咖啡        水        牛奶        茶        啤酒        
PALLMALL        PRINCE        混合烟        DUNHILL        BLUEMASTER        
-----------------------------------------------------------------------
=================================End===================================
上面运行的结果出来了,和前面的结果对比着看了看,不完全相同,其中答案1和算法二的结果完全相同,答案2、3、5、7在第一种算法的答案中也有,到头来,三种算法,有三个结果,到底哪个是最准确的算法(将所有的可能性罗列了出来),广大的网友能分析讨论,给个结论吗?本人也在研究。
  非常感谢提供代码的两位,我出于兴趣对此进行了收藏,如果谁有其它的算法,欢迎共享讨论。