现在的位置: 首页 > 综合 > 正文

爱因斯坦谜题:谁养鱼(C#版)

2011年11月13日 ⁄ 综合 ⁄ 共 6750字 ⁄ 字号 评论关闭
    一个偶然的机会再次接触到了爱因斯坦谜题,一时来了兴致,用C#做了一个程序,看看到底是谁养鱼(大学毕业后接触过这道题,不过很遗憾,那时的我没有成为2%的人,所以不知道是谁在养鱼)?

这道迷题出自1981年柏林的德国逻辑思考学院,据说世界上只有2%的人能出答案,就连大名鼎鼎的爱因斯坦也成为此题大伤脑。爱因斯坦谜题的中文表述是这样的:

1 55种颜色的房子

2 每一位房子的主人国籍都不同

3 这五个人每人只喝一个牌子的饮料,只抽一个牌子的香烟,只养一种宠物

4 没有人有相同的宠物,抽相同牌子的烟,喝相同牌子的饮料

已知条件:

1 英国人住在红房子里

2 瑞典人养了一条狗

3 丹麦人喝?

4 绿房子在白房子的左边

5 绿房子主人喝咖啡

6 pallmall烟的人养了一只鸟

7 黄房子主人抽dunhill

8 住在中间房子的人喝牛奶

9 挪威人住在第一间房子

10.抽混合烟的人住在养猫人的旁边

11.养马人住在抽dunhill烟人的旁边

12.抽bluemaster烟的人喝啤酒

13.德国人抽prince

14.挪威人住在蓝房子旁边

15.抽混合烟的人的邻居喝矿泉水

问题:谁养鱼?

很遗憾的是,这个中文表述有问题,至少有以下几方面的歧义:

一、如何区分左右?二、已知条件中的第4条,绿房子在白房子的左边,是紧邻?还是可以分开?三、第一个房子,从什么方向开始算起,左,还是右?

如果仅把上面的第二点提到的绿房子在白房子的左边,不限于紧邻,则会出现7组符合条件的组合,3个答案:丹麦人养鱼(3组)、德国人养鱼(3组)、挪威人养鱼(1组)。

这显然不符合爱因斯坦谜题的本意,所以又查了查英文原题,结果真相大白,其严谨的表述有效的消除了以上的歧义。那最终的结果究竟又如何呢?

英文原题:

The Einstein Puzzle

 

There are 5 houses in five different colors. They are lined up in a row side by side.

In each house lives a person with a different nationality.

These 5 owners drink a certain drink, smoke a certain brand of tobacco and keep a certain pet.

No owners have the same pet, smoke the same tobacco, or drink the same drink.

As you look at the 5 houses from across the street, the green house is adjacentwoog注释:adjacent adj,毗连的,邻近的,接近的;n,近邻) to the left of the white house

The Big Question is:

Who owns the Fish?

 

CLUES:

1The Brit lives in the red house

2The Swede keeps dogs as pets

3The Dane drinks tea

4The green house is on the immediate left of the white house as you stare at the front of the 5 houses

5The green house owner drinks coffee

6The person who smokes Pall Mall raises birds

7The owner of the yellow house smokes Dunhill

8The man living in the house right in the center drinks milk

9The Norwegian lives in the first house

10The man who smokes Blends lives next to the one who keeps cats

11The man who keeps horses lives next to the one who smokes Dunhill

12The owner who smokes Bluemaster drinks juice

13The German smokes Prince

14The Norwegian lives next to the blue house

15The man who smokes Blend has a neighbor who drinks water. 

相关代码如下(考虑了两种情况,当#define FastCompute时,仅有一组答案):

//[叶帆工作室] http://blog.csdn.net/yefanqiu 
#define FastCompute     
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;    

namespace Einstein
{
    
public partial class frmMain : Form
    
{
        
public frmMain()
        
{
            InitializeComponent();
        }


        
private void btnRun_Click(object sender, EventArgs e)
        
{                                                  
            Arithmetic arithmetic 
= new Arithmetic();      
            DateTime dt 
= DateTime.Now;
            
string  result = arithmetic.DoResult();
            MessageBox.Show(result 
+ "\r\n耗时:" + (DateTime.Now - dt).TotalSeconds.ToString() + "");
        }
           
    }


    
public class Arithmetic
    
{
        
string[] people   = new string[] "英国""瑞典""丹麦""挪威""德国" };
        
string[] house = new string[] """绿""""""" };
        
string[] drink = new string[] """咖啡""牛奶""啤酒""" };
        
string[] smoke = new string[] "Pall Mall""Dunhill""Blends""Blue Master""Prince" };
        
string[] pet = new string[] """""""""" };

        List
<string[]> lstCombination = new List<string[]>();   //存放全部结果(预删减后的结果)
        List<string[]> lstCombination0 = new List<string[]>();
        List
<string[]> lstCombination1 = new List<string[]>();
        List
<string[]> lstCombination2 = new List<string[]>();
        List
<string[]> lstCombination3 = new List<string[]>();
        List
<string[]> lstCombination4 = new List<string[]>();
                              
        
public string DoResult()
        
{
            
string[,] result = new string[55];

            
//生成全部的组合
            MakeCombination();
            
//预剔除不符合条件的组合
            EliminateCombination();
            
//获得有可能的组合0
            EliminateCombination0();
            
//获得有可能的组合1
            EliminateCombination1();
            
//获得有可能的组合2
            EliminateCombination2();
            
//获得有可能的组合3
            EliminateCombination3();
            
//获得有可能的组合4
            EliminateCombination4();

            
string strInfo = "";
            
int intNum = 0;

            
for (int i = 0; i < lstCombination0.Count; i++)    
            
{
                ToCombination(result, 
0, lstCombination0,i);
                
for (int j =0; j < lstCombination1.Count; j++)  
                
{
                    ToCombination(result, 
1, lstCombination1,j);
                    
for (int k = 0; k < lstCombination2.Count; k++
                    
{
                        ToCombination(result,  
2,lstCombination2, k);
                        
for (int l =0; l < lstCombination3.Count; l++)  
                        
{
                            ToCombination(result,  
3,lstCombination3, l);
                            
for (int m =0; m < lstCombination4.Count; m++
                            
{
                                ToCombination(result, 
4,lstCombination4, m);

                                
bool Flag=true;
                                
for (int e = 0; e < 5; e++)
                                
{
                                    
if (result[0, e] == result[1, e] || result[0, e] == result[2, e] || result[0, e] == result[3, e] || result[0, e] == result[4, e] ||
                                        result[
1, e] == result[2, e] || result[1, e] == result[3, e] || result[1, e] == result[4, e] ||
                                        result[
2, e] == result[3, e] || result[2, e] == result[4, e] ||
                                        result[
3, e] == result[4, e])
                                    
{
                                        Flag 
= false;
                                        
break;
                                    }

                                }

  
                                
//判断组合是否成立
                                if (Flag && Judge(result))
                                
{
                                    strInfo 
+= "---------------- " + (++intNum).ToString()+" ----------------\r\n";
                                    
for (int ii = 0; ii < 5; ii++)
                                    
{
                                        
for (int jj = 0; jj < 5; jj++)
                                        
{
                                            strInfo 
+= result[ii, jj] + " ";
                                        }

                                        strInfo 
+= "\r\n";
                                    }

#if FastCompute
                                    strInfo 
+= "------------------------------------\r\n";
                                    
return strInfo;
#endif
                                }

                            }

                        }

                    }

                }

            }


            strInfo 
+= "------------------------------------\r\n";
            
return strInfo;           
        }


        
private void ToCombination(string[,] result,int index, List<string<

抱歉!评论已关闭.