1. + expand sourceview plaincopy to clipboardprint?  
  2. //[叶帆工作室] http://yfsoft.blog.51cto.com   
  3. #define FastCompute          
  4. using System;     
  5. using System.Collections.Generic;     
  6. using System.ComponentModel;     
  7. using System.Data;     
  8. using System.Drawing;     
  9. using System.Text;     
  10. using System.Windows.Forms;     
  11. using System.Diagnostics;         
  12.     
  13. namespace Einstein     
  14. {     
  15.     public partial class frmMain : Form     
  16.     {     
  17.         public frmMain()     
  18.         {     
  19.             InitializeComponent();     
  20.         }     
  21.     
  22.         private void btnRun_Click(object sender, EventArgs e)     
  23.         {                                                       
  24.             Arithmetic arithmetic = new Arithmetic();           
  25.             DateTime dt = DateTime.Now;     
  26.             string  result = arithmetic.DoResult();     
  27.             MessageBox.Show(result + "\r\n耗时:" + (DateTime.Now - dt).TotalSeconds.ToString() + "秒");     
  28.         }                
  29.     }     
  30.     
  31.     public class Arithmetic     
  32.     {     
  33.         string[] people   = new string[] { "英国""瑞典""丹麦""挪威""德国" };     
  34.         string[] house = new string[] { "红""绿""白""黄""蓝" };     
  35.         string[] drink = new string[] { "茶""咖啡""牛奶""啤酒""水" };     
  36.         string[] smoke = new string[] { "Pall Mall""Dunhill""Blends""Blue Master""Prince" };     
  37.         string[] pet = new string[] { "狗""鸟""猫""马""鱼" };     
  38.     
  39.         List<string[]> lstCombination = new List<string[]>();   //存放全部结果(预删减后的结果)     
  40.         List<string[]> lstCombination0 = new List<string[]>();     
  41.         List<string[]> lstCombination1 = new List<string[]>();     
  42.         List<string[]> lstCombination2 = new List<string[]>();     
  43.         List<string[]> lstCombination3 = new List<string[]>();     
  44.         List<string[]> lstCombination4 = new List<string[]>();     
  45.                                    
  46.         public string DoResult()     
  47.         {     
  48.             string[,] result = new string[5, 5];     
  49.     
  50.             //生成全部的组合     
  51.             MakeCombination();     
  52.             //预剔除不符合条件的组合     
  53.             EliminateCombination();     
  54.             //获得有可能的组合0     
  55.             EliminateCombination0();     
  56.             //获得有可能的组合1     
  57.             EliminateCombination1();     
  58.             //获得有可能的组合2     
  59.             EliminateCombination2();     
  60.             //获得有可能的组合3     
  61.             EliminateCombination3();     
  62.             //获得有可能的组合4     
  63.             EliminateCombination4();     
  64.     
  65.             string strInfo = "";     
  66.             int intNum = 0;     
  67.     
  68.             for (int i = 0; i < lstCombination0.Count; i++)         
  69.             {     
  70.                 ToCombination(result, 0, lstCombination0,i);     
  71.                 for (int j =0; j < lstCombination1.Count; j++)       
  72.                 {     
  73.                     ToCombination(result, 1, lstCombination1,j);     
  74.                     for (int k = 0; k < lstCombination2.Count; k++)      
  75.                     {     
  76.                         ToCombination(result,  2,lstCombination2, k);     
  77.                         for (int l =0; l < lstCombination3.Count; l++)       
  78.                         {     
  79.                             ToCombination(result,  3,lstCombination3, l);     
  80.                             for (int m =0; m < lstCombination4.Count; m++)      
  81.                             {     
  82.                                 ToCombination(result, 4,lstCombination4, m);     
  83.     
  84.                                 bool Flag=true;     
  85.                                 for (int e = 0; e < 5; e++)     
  86.                                 {     
  87.                                     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] ||     
  88.                                         result[1, e] == result[2, e] || result[1, e] == result[3, e] || result[1, e] == result[4, e] ||     
  89.                                         result[2, e] == result[3, e] || result[2, e] == result[4, e] ||     
  90.                                         result[3, e] == result[4, e])     
  91.                                     {     
  92.                                         Flag = false;     
  93.                                         break;     
  94.                                     }     
  95.                                 }     
  96.        
  97.                                 //判断组合是否成立     
  98.                                 if (Flag && Judge(result))     
  99.                                 {     
  100.                                     strInfo += "---------------- " + (++intNum).ToString()+" ----------------\r\n";     
  101.                                     for (int ii = 0; ii < 5; ii++)     
  102.                                     {     
  103.                                         for (int jj = 0; jj < 5; jj++)     
  104.                                         {     
  105.                                             strInfo += result[ii, jj] + " ";     
  106.                                         }     
  107.                                         strInfo += "\r\n";     
  108.                                     }    
  109. #if FastCompute     
  110.                                     strInfo += "------------------------------------\r\n";     
  111.                                     return strInfo;    
  112. #endif     
  113.                                 }     
  114.                             }     
  115.                         }     
  116.                     }     
  117.                 }     
  118.             }     
  119.     
  120.             strInfo += "------------------------------------\r\n";     
  121.             return strInfo;                
  122.         }     
  123.     
  124.         private void ToCombination(string[,] result,int index, List<string[]>  lst,int num)     
  125.         {     
  126.             for (int i = 0; i < 5; i++)     
  127.             {     
  128.                 result[index, i] = lst[num][i];     
  129.             }     
  130.         }     
  131.     
  132.         //生成全部的组合     
  133.         private void MakeCombination()     
  134.         {     
  135.             string[] combination = new string[5];     
  136.     
  137.             //5*5*5*5*5=3125     
  138.             for (int i = 0; i < 5; i++) //国籍     
  139.             {     
  140.                 combination[0] = people[i];     
  141.                 for (int j = 0; j < 5; j++)  //房子     
  142.                 {     
  143.                     combination[1] = house[j];     
  144.                     for (int k = 0; k < 5; k++)  //饮料     
  145.                     {     
  146.                         combination[2] = drink[k];     
  147.                         for (int l = 0; l < 5; l++)  //香烟     
  148.                         {     
  149.                             combination[3] = smoke[l];     
  150.                             for (int m = 0; m < 5; m++)  //宠物     
  151.                             {     
  152.                                 combination[4] = pet[m];     
  153.                                 lstCombination.Add((string[])combination.Clone());     
  154.                             }     
  155.                         }     
  156.                     }     
  157.                 }     
  158.             }     
  159.         }     
  160.     
  161.         //剔除组合的判断条件     
  162.         private bool JudgeCombination(string[] combination)     
  163.         {     
  164.             //1、英国住红房子     
  165.             if (combination[0] == "英国" && combination[1] != "红"return false;     
  166.             //2、瑞典养狗     
  167.             if (combination[0] == "瑞典" && combination[4] != "狗"return false;     
  168.             //3、丹麦喝茶     
  169.             if (combination[0] == "丹麦" && combination[2] != "茶"return false;     
  170.             //5、绿房子主喝咖啡     
  171.             if (combination[1] == "绿" && combination[2] != "咖啡"return false;     
  172.             //6、抽Pall Mall香烟的养鸟     
  173.             if (combination[3] == "Pall Mall" && combination[4] != "鸟"return false;     
  174.             //7、黄房子主抽Dunhill香烟     
  175.             if (combination[1] == "黄" && combination[3] != "Dunhill"return false;     
  176.             //12、抽Blue Master的喝啤酒     
  177.             if (combination[3] == "Blue Master" && combination[2] != "啤酒"return false;     
  178.             //13、德国抽Prince香烟     
  179.             if (combination[0] == "德国" && combination[3] != "Prince"return false;     
  180.             return true;     
  181.         }     
  182.     
  183.         //预剔除不符合条件的组合     
  184.         private void EliminateCombination()     
  185.         {     
  186.             string[] combination=new string[5];     
  187.             int num=lstCombination.Count;     
  188.             int index = 0;     
  189.             while ((num--)>0)     
  190.             {     
  191.                 if (!JudgeCombination(lstCombination[index]))     
  192.                 {     
  193.                     lstCombination.RemoveAt(index);     
  194.                 }     
  195.                 else    
  196.                 {     
  197.                     index++;     
  198.                 }                     
  199.             }     
  200.         }     
  201.     
  202.         //创建组合0     
  203.         private void EliminateCombination0()     
  204.         {     
  205.             lstCombination0.Clear();      
  206.             foreach (string[] combination in lstCombination)     
  207.             {     
  208.                 //combination[1] != "红" && combination[1] != "蓝" && combination[1] != "白" && combination[1] != "绿"    
  209. #if FastCompute     
  210.                 if (combination[0] == "挪威" && combination[1] == "黄" && combination[2] != "牛奶" && combination[2] != "茶" && combination[3] != "Prince" && combination[4] != "狗")    
  211. #else     
  212.                 if (combination[0] == "挪威" && combination[1] != "红" && combination[1] != "蓝" && combination[1] != "白"  && combination[2] != "牛奶" && combination[2] != "茶" && combination[3] != "Prince" && combination[4] != "狗")    
  213. #endif     
  214.                 {               
  215.                     lstCombination0.Add(combination);     
  216.                 }                                                                            
  217.             }         
  218.         }     
  219.     
  220.         //创建组合1     
  221.         private void EliminateCombination1()     
  222.         {     
  223.             lstCombination1.Clear();     
  224.             foreach (string[] combination in lstCombination)     
  225.             {     
  226.                 if (combination[0] != "挪威" &&  combination[1] == "蓝" && combination[2] != "牛奶")     
  227.                 {     
  228.                     lstCombination1.Add(combination);     
  229.                 }     
  230.             }     
  231.         }     
  232.     
  233.         //创建组合2     
  234.         private void EliminateCombination2()     
  235.         {     
  236.             lstCombination2.Clear();     
  237.             foreach (string[] combination in lstCombination)     
  238.             {    
  239. #if FastCompute     
  240.                 if (combination[0] != "挪威" && combination[0] != "丹麦" && combination[1] != "蓝" && combination[1] != "黄" && combination[1] != "白" && combination[2] == "牛奶")    
  241. #else                    
  242.                 if (combination[0] != "挪威" && combination[0] != "丹麦" && combination[1] != "蓝"  && combination[2] == "牛奶")    
  243. #endif     
  244.                 {     
  245.                     lstCombination2.Add(combination);     
  246.                 }     
  247.             }     
  248.         }     
  249.     
  250.         //创建组合3     
  251.         private void EliminateCombination3()     
  252.         {     
  253.             lstCombination3.Clear();     
  254.             foreach (string[] combination in lstCombination)     
  255.             {    
  256. #if FastCompute     
  257.                 if (combination[0] != "挪威" && combination[1] != "黄" && combination[1] != "蓝" && combination[2] != "牛奶")    
  258. #else     
  259.                 if (combination[0] != "挪威" && combination[1] != "蓝" && combination[2] != "牛奶")    
  260. #endif     
  261.                 {     
  262.                     lstCombination3.Add(combination);     
  263.                 }     
  264.             }     
  265.         }     
  266.     
  267.         //创建组合4     
  268.         private void EliminateCombination4()     
  269.         {     
  270.             lstCombination4.Clear();     
  271.             foreach (string[] combination in lstCombination)     
  272.             {    
  273. #if FastCompute     
  274.                 if (combination[0] != "挪威" && combination[1] != "黄" && combination[1] != "蓝" && combination[1] != "绿" && combination[2] != "牛奶")    
  275. #else                    
  276.                 if (combination[0] != "挪威" && combination[1] != "蓝" && combination[1] != "绿" && combination[2] != "牛奶")    
  277. #endif     
  278.                 {     
  279.                     lstCombination4.Add(combination);     
  280.                 }     
  281.             }     
  282.         }     
  283.           
  284.         //判断     
  285.         private static bool Judge(string[,] combination)     
  286.         {                
  287.             for (int index = 0;index < 5; index++)     
  288.             {     
  289.                 //4、绿房子在白房子左面    
  290. #if FastCompute     
  291.                 if (index > 0 && combination[index, 1] == "白" && combination[index - 1, 1] != "绿"return false;    
  292. #else     
  293.                 if (combination[index, 1] == "白")     
  294.                 {     
  295.                     for (int i = index + 1; i < 5; i++)     
  296.                     {     
  297.                         if (combination[i, 1] == "绿")  //绿房子不可能出现在白房子的右边     
  298.                             return false;     
  299.                     }      
  300.                 }    
  301. #endif             
  302.                 //8、住在中间的喝牛奶     
  303.                 if (combination[2, 2] != "牛奶"return false;     
  304.                 //9、挪威住第一间房     
  305.                 if (combination[0, 0] != "挪威"return false;     
  306.                 //10、抽Blends香烟的住在养猫的隔壁     
  307.                 if (combination[index, 3] == "Blends")     
  308.                 {     
  309.                     if(!((index>0 && combination[index-1,4]=="猫") || (index<4 && combination[index+1,4]=="猫")))     
  310.                     {     
  311.                          return false;     
  312.                     }     
  313.                 }     
  314.                 //11、养马住在抽Dunhill香烟的隔壁     
  315.                 if (combination[index, 4] == "马")     
  316.                 {     
  317.                     if (!((index > 0 && combination[index - 1, 3] == "Dunhill") || (index < 4 && combination[index + 1, 3] == "Dunhill")))     
  318.                     {     
  319.                         return false;     
  320.                     }     
  321.                 }     
  322.                 //14、挪威住蓝房子隔壁     
  323.                 if (combination[index, 0] == "挪威")     
  324.                 {     
  325.                     if (!((index > 0 && combination[index - 1, 1] == "蓝") || (index < 4 && combination[index + 1, 1] == "蓝")))     
  326.                     {     
  327.                         return false;     
  328.                     }     
  329.                 }     
  330.                 //15、抽Blends香烟的人有一个喝水的邻居     
  331.                 if (combination[index, 3] == "Blends")     
  332.                 {     
  333.                     if (!((index > 0 && combination[index - 1, 2] == "水") || (index < 4 && combination[index + 1, 2] == "水")))     
  334.                     {     
  335.                         return false;     
  336.                     }     
  337.                 }     
  338.             }              
  339.     
  340.             return true;     
  341.         }     
  342.     
  343.     }     
  344. }    

最终的计算结果如下(7组结果由于不合理,故省略,有兴趣的朋友可以自己把上面的代码运行一下):

-----------------------------------

挪威 黄 水 Dunhill 猫  

丹麦 蓝 茶 Blends 马      

英国 红 牛奶 Pall Mall 鸟

德国 绿 咖啡 Prince 鱼    

瑞典 白 啤酒 Blue Master 狗  

-----------------------------------

耗时:115.15625秒

 

如果大家对手动计算感兴趣,下面的文章写的不错,可以参考一下:

http://www.cnblogs.com/terryli/archive/2008/04/06/1138788.html

 

此外大家如果有更好的算法,不妨拿出来秀一秀!