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

简介:

 

 
  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

 

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









本文转自yefanqiu51CTO博客,原文链接:http://blog.51cto.com/yfsoft/324083,如需转载请自行联系原作者

相关文章
|
2月前
|
开发框架 前端开发 .NET
C#编程与Web开发
【4月更文挑战第21天】本文探讨了C#在Web开发中的应用,包括使用ASP.NET框架、MVC模式、Web API和Entity Framework。C#作为.NET框架的主要语言,结合这些工具,能创建动态、高效的Web应用。实际案例涉及企业级应用、电子商务和社交媒体平台。尽管面临竞争和挑战,但C#在Web开发领域的前景将持续拓展。
|
2月前
|
SQL 开发框架 安全
C#编程与多线程处理
【4月更文挑战第21天】探索C#多线程处理,提升程序性能与响应性。了解C#中的Thread、Task类及Async/Await关键字,掌握线程同步与安全,实践并发计算、网络服务及UI优化。跟随未来发展趋势,利用C#打造高效应用。
|
28天前
|
存储 C# 开发者
C# 编程基础:注释、变量、常量、数据类型和自定义类型
C# 编程基础:注释、变量、常量、数据类型和自定义类型
22 1
|
2月前
|
开发框架 .NET Java
探索 C#编程的奥秘与魅力
【4月更文挑战第20天】C#是微软开发的现代、面向对象的编程语言,以其简洁语法、强大功能和跨平台支持脱颖而出。它支持自动垃圾回收、泛型、委托、LINQ,并广泛应用于桌面、Web、移动和游戏开发。C#拥有活跃的开发者社区和丰富的资源,是Unity游戏开发的首选语言。随着.NET Core,C#可在多个操作系统上运行,持续创新,未来发展潜力巨大。
|
2月前
|
存储 安全 网络安全
C#编程的安全性与加密技术
【4月更文挑战第21天】C#在.NET框架支持下,以其面向对象和高级特性成为安全软件开发的利器。本文探讨C#在安全加密领域的应用,包括使用System.Security.Cryptography库实现加密算法,利用SSL/TLS保障网络传输安全,进行身份验证,并强调编写安全代码的重要性。实际案例涵盖在线支付、企业应用和文件加密,展示了C#在应对安全挑战的同时,不断拓展其在该领域的潜力和未来前景。
|
2月前
|
人工智能 C# 云计算
C#编程的未来发展趋向
【4月更文挑战第21天】C#编程未来将深化跨平台支持,强化云计算与容器技术集成,如.NET Core、Docker。在AI和ML领域,C#将提供更丰富框架,与AI芯片集成。语言和工具将持续创新,优化异步编程,如Task、async和await,提升多核性能。开源生态的壮大将吸引更多开发者,共创更多机遇。
|
2月前
|
程序员 C#
C#编程中的面向对象编程思想
【4月更文挑战第21天】本文探讨了C#中的面向对象编程,包括类、对象、封装、继承和多态。类是对象的抽象,定义属性和行为;对象是类的实例。封装隐藏内部细节,只暴露必要接口。继承允许类复用和扩展属性与行为,而多态使不同类的对象能通过相同接口调用方法。C#通过访问修饰符实现封装,使用虚方法和抽象方法实现多态。理解并应用这些概念,能提升代码的清晰度和可扩展性,助你成为更好的C#程序员。
|
2月前
|
开发框架 安全 .NET
C#编程高手的成长之路
【4月更文挑战第21天】本文揭示了成为C#编程高手的路径:牢固掌握基础知识和面向对象编程,深入了解C#特性如泛型和委托,精通ASP.NET等框架工具,养成良好编程习惯,持续学习实践并参与开源项目,勇于挑战创新。通过这些步骤,不断提升编程技能,迈向C#编程的巅峰。