- + expand sourceview plaincopy to clipboardprint?
-
- #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[5, 5];
-
-
- MakeCombination();
-
- EliminateCombination();
-
- EliminateCombination0();
-
- EliminateCombination1();
-
- EliminateCombination2();
-
- EliminateCombination3();
-
- 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[]> lst,int num)
- {
- for (int i = 0; i < 5; i++)
- {
- result[index, i] = lst[num][i];
- }
- }
-
-
- private void MakeCombination()
- {
- string[] combination = new string[5];
-
-
- for (int i = 0; i < 5; i++)
- {
- combination[0] = people[i];
- for (int j = 0; j < 5; j++)
- {
- combination[1] = house[j];
- for (int k = 0; k < 5; k++)
- {
- combination[2] = drink[k];
- for (int l = 0; l < 5; l++)
- {
- combination[3] = smoke[l];
- for (int m = 0; m < 5; m++)
- {
- combination[4] = pet[m];
- lstCombination.Add((string[])combination.Clone());
- }
- }
- }
- }
- }
- }
-
-
- private bool JudgeCombination(string[] combination)
- {
-
- if (combination[0] == "英国" && combination[1] != "红") return false;
-
- if (combination[0] == "瑞典" && combination[4] != "狗") return false;
-
- if (combination[0] == "丹麦" && combination[2] != "茶") return false;
-
- if (combination[1] == "绿" && combination[2] != "咖啡") return false;
-
- if (combination[3] == "Pall Mall" && combination[4] != "鸟") return false;
-
- if (combination[1] == "黄" && combination[3] != "Dunhill") return false;
-
- if (combination[3] == "Blue Master" && combination[2] != "啤酒") return false;
-
- if (combination[0] == "德国" && combination[3] != "Prince") return false;
- return true;
- }
-
-
- private void EliminateCombination()
- {
- string[] combination=new string[5];
- int num=lstCombination.Count;
- int index = 0;
- while ((num--)>0)
- {
- if (!JudgeCombination(lstCombination[index]))
- {
- lstCombination.RemoveAt(index);
- }
- else
- {
- index++;
- }
- }
- }
-
-
- private void EliminateCombination0()
- {
- lstCombination0.Clear();
- foreach (string[] combination in lstCombination)
- {
-
- #if FastCompute
- if (combination[0] == "挪威" && combination[1] == "黄" && combination[2] != "牛奶" && combination[2] != "茶" && combination[3] != "Prince" && combination[4] != "狗")
- #else
- if (combination[0] == "挪威" && combination[1] != "红" && combination[1] != "蓝" && combination[1] != "白" && combination[2] != "牛奶" && combination[2] != "茶" && combination[3] != "Prince" && combination[4] != "狗")
- #endif
- {
- lstCombination0.Add(combination);
- }
- }
- }
-
-
- private void EliminateCombination1()
- {
- lstCombination1.Clear();
- foreach (string[] combination in lstCombination)
- {
- if (combination[0] != "挪威" && combination[1] == "蓝" && combination[2] != "牛奶")
- {
- lstCombination1.Add(combination);
- }
- }
- }
-
-
- private void EliminateCombination2()
- {
- lstCombination2.Clear();
- foreach (string[] combination in lstCombination)
- {
- #if FastCompute
- if (combination[0] != "挪威" && combination[0] != "丹麦" && combination[1] != "蓝" && combination[1] != "黄" && combination[1] != "白" && combination[2] == "牛奶")
- #else
- if (combination[0] != "挪威" && combination[0] != "丹麦" && combination[1] != "蓝" && combination[2] == "牛奶")
- #endif
- {
- lstCombination2.Add(combination);
- }
- }
- }
-
-
- private void EliminateCombination3()
- {
- lstCombination3.Clear();
- foreach (string[] combination in lstCombination)
- {
- #if FastCompute
- if (combination[0] != "挪威" && combination[1] != "黄" && combination[1] != "蓝" && combination[2] != "牛奶")
- #else
- if (combination[0] != "挪威" && combination[1] != "蓝" && combination[2] != "牛奶")
- #endif
- {
- lstCombination3.Add(combination);
- }
- }
- }
-
-
- private void EliminateCombination4()
- {
- lstCombination4.Clear();
- foreach (string[] combination in lstCombination)
- {
- #if FastCompute
- if (combination[0] != "挪威" && combination[1] != "黄" && combination[1] != "蓝" && combination[1] != "绿" && combination[2] != "牛奶")
- #else
- if (combination[0] != "挪威" && combination[1] != "蓝" && combination[1] != "绿" && combination[2] != "牛奶")
- #endif
- {
- lstCombination4.Add(combination);
- }
- }
- }
-
-
- private static bool Judge(string[,] combination)
- {
- for (int index = 0;index < 5; index++)
- {
-
- #if FastCompute
- if (index > 0 && combination[index, 1] == "白" && combination[index - 1, 1] != "绿") return false;
- #else
- if (combination[index, 1] == "白")
- {
- for (int i = index + 1; i < 5; i++)
- {
- if (combination[i, 1] == "绿")
- return false;
- }
- }
- #endif
-
- if (combination[2, 2] != "牛奶") return false;
-
- if (combination[0, 0] != "挪威") return false;
-
- if (combination[index, 3] == "Blends")
- {
- if(!((index>0 && combination[index-1,4]=="猫") || (index<4 && combination[index+1,4]=="猫")))
- {
- return false;
- }
- }
-
- if (combination[index, 4] == "马")
- {
- if (!((index > 0 && combination[index - 1, 3] == "Dunhill") || (index < 4 && combination[index + 1, 3] == "Dunhill")))
- {
- return false;
- }
- }
-
- if (combination[index, 0] == "挪威")
- {
- if (!((index > 0 && combination[index - 1, 1] == "蓝") || (index < 4 && combination[index + 1, 1] == "蓝")))
- {
- return false;
- }
- }
-
- if (combination[index, 3] == "Blends")
- {
- if (!((index > 0 && combination[index - 1, 2] == "水") || (index < 4 && combination[index + 1, 2] == "水")))
- {
- return false;
- }
- }
- }
-
- return true;
- }
-
- }
- }
最终的计算结果如下(7组结果由于不合理,故省略,有兴趣的朋友可以自己把上面的代码运行一下):
-----------------------------------
挪威 黄 水 Dunhill 猫
丹麦 蓝 茶 Blends 马
英国 红 牛奶 Pall Mall 鸟
德国 绿 咖啡 Prince 鱼
瑞典 白 啤酒 Blue Master 狗
-----------------------------------
耗时:115.15625秒
如果大家对手动计算感兴趣,下面的文章写的不错,可以参考一下:
此外大家如果有更好的算法,不妨拿出来秀一秀!
本文转自yefanqiu51CTO博客,原文链接:http://blog.51cto.com/yfsoft/324083,如需转载请自行联系原作者