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

最终的计算结果如下(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,如需转载请自行联系原作者

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

  1. 爱因斯坦谜题 谁养鱼 C 版

    一个偶然的机会再次接触到了爱因斯坦谜题,一时来了兴致,用C#做了一个程序,看看到底是谁养鱼(大学毕业后接触过这道题,不过很遗憾,那时的我没有成为2%的人,所以不知道是谁在养鱼)? 这道迷题出自1981 ...

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

    一个偶然的机会再次接触到了爱因斯坦谜题,一时来了兴致,用C#做了一个程序,看看到底是谁养鱼(大学毕业后接触过这道题,不过很遗憾,那时的我没有成为2%的人,所以不知道是谁在养鱼)? 这道迷题出自1981 ...

  3. C语言--爱因斯坦谜题

    尊重原创:http://blog.csdn.net/aspirationflow/article/details/7748364 C语言编程快速解决爱因斯坦谜题 2012年7月15日 1      问 ...

  4. C语言编程快速解决爱因斯坦谜题

    C语言编程快速解决爱因斯坦谜题 2012年7月15日 1      问题简介 爱因斯坦谜题(Einstein'sriddle)是很多人熟悉的问题.记得读高中时候就有同学就拿这个题目来考人,不过那时的解 ...

  5. 爱因斯坦谜题解答(三种算法比较)

    爱因斯坦谜题:     在一条街上有颜色互不相同的五栋房子,不同国籍的人分别住在这五栋房子力,每人抽不同品牌的香烟,喝不同的饮料,养不同的宠物.已知如下情况: 1.  英国人住红色房子里. 2.  瑞 ...

  6. 爱因斯坦谜题的真正答案

    爱因斯坦谜题的真正答案 前几天看博客的时候,发现有人发了一个解决爱因斯坦的谜题的文章. http://www.cnblogs.com/yefanqiu/archive/2009/09/27/15753 ...

  7. 【爱因斯坦谜题】用python基础语法解决爱因斯坦谜题

    今天分享一个困难的谜题 据传是爱因斯坦还年轻时所想出来的 本人自学python已经有一段时间,偶然间了解到了爱因斯坦谜题,就想用已经学过的知识来解决这个谜题,下面我就将我的方法分享给大家. 题目: 有 ...

  8. 苹果开发者账号网页版续费失败支付报错解决办法

    背景: 近日,有一个公司类型的苹果开发者账号老号(新号都是在Apple Developer app完成自动续期)即将到期需要续费,但是一直拖着只剩10天时间才准备续,却发现无法正常完成续费. 问题: ...

  9. 重构-打造爱因斯坦谜题最快算法

    上一篇里,阐述了解这道题的思路,并在代码上实现.不过代码还有很多可改进之处.性能方面,虽然比穷举法快得多,此外搜索算法还是比较盲目,效率应该能更上一层楼. 首先是在算法实现最后一步的搜索树递归方法中, ...

最新文章

  1. Windows Phone 7 IEnumerableT.Select和SelectMany的区别
  2. 如何在TypeScript中删除数组项?
  3. WinForm经典窗体皮肤[重绘]
  4. 数学建模——TOPSIS综合评价模型Python代码
  5. java web中验证码的实现
  6. 华为S2326 TP-EI交换机如何做端口镜像
  7. 阿里云ACE共创空间——大数据方案体验1 日志服务
  8. java utf8 byte_byte以及UTF-8的转码规则
  9. 黑客攻防技术宝典Web实战篇第2版—第3章 Web应用程序技术
  10. 你知道K8S暴露服务的方式有哪些吗?
  11. python中linspace函数_numpy.linspace函数具体使用详解
  12. C++普通类继承模版类demo
  13. JPDA 架构研究19 - JDI的连接模块
  14. 斗鱼直播分享html代码,用纯javascript实现斗鱼直播弹幕效果,代码也才这么点-优酷弹幕怎么设置...
  15. MATLAB偏微分方程数值解视频课程
  16. 如何不用u盘将32位Windows转为64位Windows
  17. Microhard P900 900MHz跳频电台核心模块
  18. 现代信号处理——盲信号分离(盲信号分离的基本理论)
  19. 对日软件工程师的几种招聘要求
  20. 洛谷P1338(末日的传说)

热门文章

  1. 在配置spring配置文件时各种报错,解决办法
  2. 数据库MySQL- 查询结果去重【distinct】
  3. Python的类对象
  4. 网络机顶盒哪个好?发烧友实测2023网络机顶盒排名
  5. leetcode解题方案--015--3 sum
  6. JSON-SCHEMA + AJV的坑
  7. win10 蓝牙耳机 麦克风不可用
  8. mini-ui 中的message弹出框中点击确定与取消之后使用回调函数的方法
  9. 苏宁减持阿里股票获利32亿;京东物流宣称已盈利;三星去年研发投入143亿美元丨价值早报
  10. Java SpringBoot 集成微信公众号