小马

现在是零将来是无限

导航

Einstein's Riddle 爱因斯坦出的智力题?

现有题号称爱因斯坦出的智力题全世界只有2%能够做出。
------------------------------------------------
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香烟的人有一个喝水的邻居

为了便于消除翻译的误差,先将英文版本贴出:

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?

~

现在请你用c#写出解这个问题的程序!

相信不少人对此题的第一个想法是通过暴力算法穷举所有可能让计算机进行求解。如果是这样那么你不用辛苦了,下面早就有人这么干过了:
暴力算法解益智题(c#2.0版本):http://www.webasp.net/article/27/26094.htm
C#轻松解决世纪迷题 :http://tech.ccidnet.com/pub/article/c1081_a94061_p1.html
这些解法虽然都是对的,但是正如有人评论道:用笔解这题时候可比用程序看起来省力多了........算的上“轻松”吗?
在这里向大家隆重推荐香港城市大学计算机科学院的陈汉伟博士(Dr. Andy Chun, Hon Wai)的人工智能工具 NSolver
用NSolver如何解决问题?
先举一个简单的例子:
IQ 题:
    农场里有兔子和鸡若干只,知道共20个头和56条腿。问兔子和鸡各多少只?
用c#来解这个问题,可能是:

using System;
public class Rabbit 
{
    
public int rabbit = 0;
    
public int pheasant = 0;
    
public void Run() 
    
{
        
for (rabbit = 0; rabbit<=20; rabbit++)
            
for (pheasant = 0; pheasant<=20; pheasant++)
                
if (rabbit+pheasant==20
                    
&& rabbit*4+pheasant*2==56)
                    Console.WriteLine(
                        
"Rabbit["+rabbit+"] Pheasant["+pheasant+"]");
    }

    
public static void Main() {(new Rabbit()).Run();}
}

有什么不足?

    1、没有变量/未知量的概念
    2、没有什么值合法什么不合法的概念(问题域)
    3、没有变量间的关系/约束概念
    4、所有都Hardcode了,不能扩展到更复杂的问题
    5、暴力破解穷举

比较一下NSolver的解法:

using ai.net.Solver;
using System;
public class Rabbit : Solver 
{
    
public Var rabbits, pheasants;
    
public void Run() 
    
{
        rabbits 
= var(020"Rabbits");
        pheasants 
= var(020"Pheasants");
        Post(rabbits.Sum(pheasants).Eq(
20));
        Post(rabbits.Prod(
4).Sum(pheasants.Prod(2)).Eq(56));
        Console.WriteLine(rabbits
+" "+pheasants);
    }

    
public static void Main() {(new Rabbit()).Run();}
}


现在用NSolver解决爱因斯坦问题:

using System;
using System.Text;
using ai.net.Solver;
using System.Diagnostics;
using System.Collections;

class Einstein : Solver
{
    
public void Run()
    
{
        
// the 房子s
        Var 红房子 = var(04"红房子");
        Var 绿房子 
= var(04"绿房子");
        Var 白房子 
= var(04"白房子");
        Var 黄房子 
= var(04"黄房子");
        Var 蓝房子 
= var(04"蓝房子");
        VarList all房子s 
= varList(红房子, 绿房子, 白房子, 黄房子, 蓝房子);

        
// nationalities
        Var 英国人 = var(04"英国人");
        Var 瑞典人 
= var(04"瑞典人");
        Var 丹麦人 
= var(04"丹麦人");
        Var 挪威人 
= var(04"挪威人");
        Var 德国人 
= var(04"德国人");
        VarList allNationalities 
= varList(英国人, 瑞典人, 丹麦人, 挪威人, 德国人);

        
// beverages
        Var 茶 = var(04"");
        Var 咖啡 
= var(04"咖啡");
        Var 牛奶 
= var(04"牛奶");
        Var 啤酒 
= var(04"啤酒");
        Var 矿泉水 
= var(04"矿泉水");
        VarList allBeverages 
= varList(茶, 咖啡, 牛奶, 啤酒, 矿泉水);

        
// cigarettes
        Var pallMall = var(04"Pall Mall");
        Var dunhill 
= var(04"Dunhill");
        Var 混合烟 
= var(04"混合烟");
        Var BlueMasters 
= var(04"BlueMasters");
        Var Prince 
= var(04"Prince");
        VarList allCigs 
= varList(pallMall, dunhill, 混合烟, BlueMasters, Prince);

        
// pets
        Var 狗 = var(04"");
        Var 鸟 
= var(04"");
        Var 鱼 
= var(04"");// <-- Who owns the 鱼?
        Var 马 = var(04"");
        Var 猫 
= var(04""); 
        VarList allPets 
= varList(狗, 鸟, 鱼, 马, 猫);


        
// More clues
        Post(红房子.Eq(英国人));
        Post(狗.Eq(瑞典人));
        Post(茶.Eq(丹麦人));
        
//The 绿 房子 is on the left of the 白 房子 
        Var gLeft = 白房子.Diff(绿房子);
        gLeft.SetRange(
01);
        Post(gLeft.Neq(
0));


        Post(绿房子.Eq(咖啡));
        Post(pallMall.Eq(鸟));
        Post(黄房子.Eq(dunhill));
        Post(牛奶.Eq(
2));
        Post(挪威人.Eq(
0));

        Var 混合烟Vs鱼 
= 鱼.Diff(混合烟);
        混合烟Vs鱼.SetRange(
-11);
        Post(混合烟Vs鱼.Neq(
0));

        Var 马VsDunhill 
= 马.Diff(dunhill);
        马VsDunhill.SetRange(
-11);
        Post(马VsDunhill.Neq(
0));

        Post(BlueMasters.Eq(啤酒));
        Post(Prince.Eq(德国人));

        Var 蓝Vs挪威人 
= 蓝房子.Diff(挪威人);
        蓝Vs挪威人.SetRange(
-11);
        Post(蓝Vs挪威人.Neq(
0));

        Var 混合烟Vs矿泉水 
= 混合烟.Diff(矿泉水);
        混合烟Vs矿泉水.SetRange(
-11);
        Post(混合烟Vs矿泉水.Neq(
0));

        Post(AllDiff(all房子s));
        Post(AllDiff(allNationalities));
        Post(AllDiff(allBeverages));
        Post(AllDiff(allCigs));
        Post(AllDiff(allPets));

        Activate(all房子s);
        Activate(allNationalities);
        Activate(allBeverages);
        Activate(allCigs);
        Activate(allPets);

        
while (NextSolution())
        
{
            Console.WriteLine(allNationalities);
            Console.WriteLine(all房子s);
            Console.WriteLine(allBeverages);
            Console.WriteLine(allCigs);
            Console.WriteLine(allPets);
        }

    }


    
public static void Main()
    
{
        Einstein solver 
= new Einstein();
        solver.Run();
    }

}


看看这一行行代码吧,多么简洁易懂! 现在你要做的就是陈述问题,NSolver帮你做剩下的!
延伸阅读
         
http://sc.info.gov.hk/gb/www.ugc.edu.hk/rgc/rgcnews9/Pages/6%20AI-C.html
NSolver的好处就是,它不仅仅能解决这个爱因斯坦的问题,它所能解决的是属于一个叫做“Constraint Satisfaction Problems”的一大堆问题。 
      我这里再给大家出个类似的题目,请你用NSolver来解决它:
 Our problem consists of three mice living next to each other in three holes in the wall. Each mouse has a favorite cheese flavor and a favorite TV show. Here are the hints:

  1. Mickey Mouse loves Gouda
  2. Mighty Mouse's favorite TV show is Emergency Room
  3. The mouse that lives in the left hole never misses an episode of Seinfeld
  4. Mickey Mouse and Mighty Mouse have one mouse hole between them
  5. The Simpsons fan does not live on the left of the Brie lover

posted on 2005-09-17 14:42  mahope  阅读(4647)  评论(14编辑  收藏  举报