思路,理论不太会写,要看理论可看: https://blog.csdn.net/orbit/article/details/6994575;https://blog.csdn.net/maidou0921/article/details/51910075
在这里,我就直接贴代码:
namespace 爱因斯坦思考题
{
class Program
{
static people[] peoples = null;
static color[] colorList = null;
static citizenship[] citishipList = null;
static drink[] drinkList = null;
static cigarette[] cigareList = null;
static pet[] petList = null;
static List<people[]> peoplesList = new List<people[]>();
static void Main(string[] args)
{
int num = 5;
peoples = new people[num];
colorList = InitTList<color>(num);
citishipList = InitTList<citizenship>(num);
drinkList = InitTList<drink>(num);
cigareList = InitTList<cigarette>(num);
petList = InitTList<pet>(num);
GetPeoplesList(peoples, 0, num, colorList, citishipList, drinkList, cigareList, petList);
Write();
}
#region 初始化
static hourse[] InitHourseList(int num)
{
hourse[] h = new hourse[num];
for (int i = 0; i < num; i++)
{
h[i] = new hourse(i + 1);
}
return h;
}
static T[] InitTList<T>(int num)
{
T[] t = new T[num];
Type type = typeof(T);
for (int i = 0; i < num; i++)
{
object obj = Activator.CreateInstance(type, new object[] { i });
t[i] = (T)obj;
}
return t;
}
#endregion
#region 测试条件
/// <summary>
/// 条件01-英国人住红色房子
/// </summary>
/// <param name="p">任意人</param>
/// <returns>是否符合</returns>
static bool Test01(people p)
{
if (citishipList[p.citizenshipNumber].name == "英国人")
{
if (colorList[p.ColorNumber].name == "红色")
return true;
return false;
}
return true;
}
/// <summary>
/// 条件02-瑞典人养狗
/// </summary>
/// <param name="p">任意人</param>
/// <returns>是否符合</returns>
static bool Test02(people p)
{
if (citishipList[p.citizenshipNumber].name == "瑞典人")
{
if (petList[p.petNumber].name == "狗")
return true;
return false;
}
return true;
}
/// <summary>
/// 条件03-丹麦人喝茶
/// </summary>
/// <param name="p">任意人</param>
/// <returns>是否符合</returns>
static bool Test03(people p)
{
if (citishipList[p.citizenshipNumber].name == "丹麦人")
{
if (drinkList[p.drinkNumber].name == "茶")
return true;
return false;
}
return true;
}
/// <summary>
/// 条件04-绿色房子在白色房子左边
/// </summary>
/// <param name="p"></param>
/// <param name="isLeftToRight">是否是从左到右编号</param>
/// <returns></returns>
static bool Test04(people p, people[] list, bool isLeftToRight = true)
{
if (isLeftToRight)
{
//这里是通过确定房子编号是从左到右编号
if (colorList[p.ColorNumber].name == "白色")
{
int num = p.houseNumber - 1;
if (num < 0) return false;
people p2 = GetPeopleByHourse(num, list);
if (p2.houseNumber == -1) return false;
if (colorList[p2.ColorNumber].name == "绿色")
return true;
return false;
}
}
else
{
//这里是通过确定房子编号是从右到左编号
if (colorList[p.ColorNumber].name == "绿色")
{
int num = p.houseNumber - 1;
if (num < 0) return false;
people p2 = GetPeopleByHourse(num, list);
if (p2.houseNumber == -1) return false;
if (colorList[p2.ColorNumber].name == "白色")
return true;
return false;
}
}
return true;
}
/// <summary>
/// 条件05-绿房子主人喝咖啡
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
static bool Test05(people p)
{
if (colorList[p.ColorNumber].name == "绿色")
{
if (drinkList[p.drinkNumber].name == "咖啡")
return true;
return false;
}
return true;
}
/// <summary>
/// 条件06-抽PallMall香烟的人养鸟
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
static bool Test06(people p)
{
if (cigareList[p.cigaretteNumber].name == "PallMall")
{
if (petList[p.petNumber].name == "鸟")
return true;
return false;
}
return true;
}
/// <summary>
/// 条件07-黄色房子的主人抽Dunhill香烟
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
static bool Test07(people p)
{
if (colorList[p.ColorNumber].name == "黄色")
{
if (cigareList[p.cigaretteNumber].name == "Dunhill")
return true;
return false;
}
return true;
}
/// <summary>
/// 条件08-住在中间房子的人喝牛奶
/// </summary>
/// <param name="p">住在中间房子的人</param>
/// <returns>是否符合</returns>
static bool Test08(people p)
{
if (drinkList[p.drinkNumber].name == "牛奶")
return true;
return false;
}
/// <summary>
/// 条件09-挪威人住第一间房
/// </summary>
/// <param name="p">住第一间房的人</param>
/// <returns>是否符合</returns>
static bool Test09(people p)
{
if (citishipList[p.citizenshipNumber].name == "挪威人")
return true;
return false;
}
/// <summary>
/// 条件10-抽Bleeds香烟的人住在养猫的人隔壁
/// </summary>
/// <param name="p">抽Bleeds香烟的人</param>
/// <returns></returns>
static bool Test10(people p, people[] list)
{
int num1 = p.houseNumber - 1;
if (num1 >= 0)
{
people p2 = GetPeopleByHourse(num1, list);
if (petList[p2.petNumber].name == "猫")
return true;
}
int num2 = p.houseNumber + 1;
if (num2 < list.Length)
{
people p2 = GetPeopleByHourse(num2, list);
if (petList[p2.petNumber].name == "猫")
return true;
}
return false;
}
/// <summary>
/// 条件11-养马的人住抽Dunhill香烟的人隔壁
/// </summary>
/// <param name="list">人群</param>
/// <returns></returns>
static bool Test11(people[] list)
{
foreach (people p in list)
{
if (cigareList[p.cigaretteNumber].name == "Dunhill")
{
int num1 = p.houseNumber - 1;
if (num1 >= 0)
{
people p2 = GetPeopleByHourse(num1, list);
if (petList[p2.petNumber].name == "马")
return true;
}
int num2 = p.houseNumber + 1;
if (num2 < list.Length)
{
people p2 = GetPeopleByHourse(num2, list);
if (petList[p2.petNumber].name == "马")
return true;
}
return false;
}
}
return false;
}
/// <summary>
/// 条件12-抽BlueMaster的人喝啤酒
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
static bool Test12(people p)
{
if (cigareList[p.cigaretteNumber].name == "BlueMaster")
{
if (drinkList[p.drinkNumber].name == "啤酒")
return true;
return false;
}
return true;
}
/// <summary>
/// 条件13-德国人抽Prince香烟
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
static bool Test13(people p)
{
if (cigareList[p.cigaretteNumber].name == "Prince")
{
if (citishipList[p.citizenshipNumber].name == "德国人")
return true;
return false;
}
return true;
}
/// <summary>
/// 条件14-挪威人住蓝色房子隔壁
/// </summary>
/// <param name="list">人群</param>
/// <returns></returns>
static bool Test14(people[] list)
{
foreach (people p in list)
{
if (colorList[p.ColorNumber].name == "蓝色")
{
int num1 = p.houseNumber - 1;
if (num1 >= 0)
{
people p2 = GetPeopleByHourse(num1, list);
if (citishipList[p2.citizenshipNumber].name == "挪威人")
return true;
}
int num2 = p.houseNumber + 1;
if (num2 < list.Length)
{
people p2 = GetPeopleByHourse(num2, list);
if (citishipList[p2.citizenshipNumber].name == "挪威人")
return true;
}
return false;
}
}
return false;
}
/// <summary>
/// 条件15-抽Bleeds香烟的人有一个喝水的邻居
/// </summary>
/// <param name="p">抽Bleeds香烟的人</param>
/// <returns></returns>
static bool Test15(people p, people[] list)
{
int num1 = p.houseNumber - 1;
if (num1 >= 0)
{
people p2 = GetPeopleByHourse(num1, list);
if (drinkList[p2.drinkNumber].name == "水")
return true;
}
int num2 = p.houseNumber + 1;
if (num2 < list.Length)
{
people p2 = GetPeopleByHourse(num2, list);
if (drinkList[p2.drinkNumber].name == "水")
return true;
}
return false;
}
/// <summary>
/// 条件10和15-抽Bleeds香烟的人有一个喝水的邻居同时又有一个养猫的邻居
/// </summary>
/// <param name="list">人群</param>
/// <returns></returns>
static bool Test10and15(people[] list)
{
foreach (people p in list)
{
if (cigareList[p.cigaretteNumber].name == "Bleeds")
{
if (Test10(p, list) && Test15(p, list)) return true;
else return false;
}
}
return false;
}
#endregion
#region 方法
static void GetPeoplesList(people[] list, int index, int num, color[] colorList, citizenship[] citiList, drink[] drinkList, cigarette[] cigareList, pet[] petList)
{
if (index == num)
{
if (!Test10and15(list)) return;
if (!Test11(list)) return;
if (!Test14(list)) return;
peoplesList.Add(PeoplesCopy(list));
return;
}
people p = new people();
p.houseNumber = index;
for (int j = 0; j < num; j++)
{
if (colorList[j].used) continue;
p.ColorNumber = j;
if (!Test04(p, list)) continue;
colorList[j].used = true;
for (int k = 0; k < num; k++)
{
if (citiList[k].used) continue;
p.citizenshipNumber = k;
if (index == 0 && !Test09(p)) continue;
if (!Test01(p)) continue;
citiList[k].used = true;
for (int l = 0; l < num; l++)
{
if (drinkList[l].used) continue;
p.drinkNumber = l;
if (index == num / 2 && !Test08(p)) continue;
if (!Test03(p)) continue;
if (!Test05(p)) continue;
drinkList[l].used = true;
for (int m = 0; m < num; m++)
{
if (cigareList[m].used) continue;
p.cigaretteNumber = m;
if (!Test07(p)) continue;
if (!Test13(p)) continue;
cigareList[m].used = true;
for (int n = 0; n < num; n++)
{
if (petList[n].used) continue;
p.petNumber = n;
if (!Test02(p)) continue;
if (!Test06(p)) continue;
if (!Test12(p)) continue;
petList[n].used = true;
list[index] = p;
GetPeoplesList(list, index + 1, num, colorList, citiList, drinkList, cigareList, petList);
petList[n].used = false;
}
cigareList[m].used = false;
}
drinkList[l].used = false;
}
citiList[k].used = false;
}
colorList[j].used = false;
}
}
static people GetPeopleByHourse(int hourseNumber, people[] list)
{
people newP = new people();
foreach (people p in list)
{
if (p.houseNumber == hourseNumber)
{
newP.houseNumber = p.houseNumber;
newP.ColorNumber = p.ColorNumber;
newP.citizenshipNumber = p.citizenshipNumber;
newP.drinkNumber = p.drinkNumber;
newP.cigaretteNumber = p.cigaretteNumber;
newP.petNumber = p.petNumber;
return newP;
}
}
return newP;
}
static people[] PeoplesCopy(people[] list)
{
int len = list.Length;
people[] newps = new people[len];
for (int i = 0; i < len; i++)
{
people newp = new people()
{
houseNumber = list[i].houseNumber,
ColorNumber = list[i].ColorNumber,
citizenshipNumber = list[i].citizenshipNumber,
drinkNumber = list[i].drinkNumber,
cigaretteNumber = list[i].cigaretteNumber,
petNumber = list[i].petNumber
};
newps[i] = newp;
}
return newps;
}
static void Write()
{
for (int i = 0, len = peoplesList.Count; i < len; i++)
{
Console.WriteLine("第{0}种可能性:", i + 1);
foreach (people p in peoplesList[i])
{
Console.WriteLine(p.houseNumber + 1 + "号房子是{0}的,住着{1},喜欢喝{2},喜欢抽{3},养{4}",
colorList[p.ColorNumber].name, citishipList[p.citizenshipNumber].name, drinkList[p.drinkNumber].name,
cigareList[p.cigaretteNumber].name, petList[p.petNumber].name);
}
}
Console.ReadLine();
}
#endregion
}
#region 辅助类
class people
{
public int houseNumber;
public int ColorNumber;
public int citizenshipNumber;
public int drinkNumber;
public int cigaretteNumber;
public int petNumber;
public people()
{
houseNumber = -1;
}
}
class hourse
{
public int number;
public bool used;
public hourse(int num)
{
// TODO: Complete member initialization
this.number = num;
used = false;
}
}
class color
{
public string name;
public bool used;
private string[] colors = new string[] { "红色", "绿色", "蓝色", "黄色", "白色", "紫色", "粉色", "黑色", "灰色" };
public color(int num)
{
// TODO: Complete member initialization
this.name = colors[num];
used = false;
}
}
class citizenship
{
public string name;
public bool used;
private string[] citizenships = new string[] { "丹麦人", "挪威人", "英国人", "德国人", "瑞典人", "韩国人", "印度人", "法国人", "朝鲜人" };
public citizenship(int num)
{
// TODO: Complete member initialization
this.name = citizenships[num];
used = false;
}
}
class drink
{
public string name;
public bool used;
private string[] drinks = new string[] { "水", "茶", "牛奶", "咖啡", "啤酒", "奶茶", "奶酒", "可乐", "酸奶" };
public drink(int num)
{
// TODO: Complete member initialization
this.name = drinks[num];
used = false;
}
}
class cigarette
{
public string name;
public bool used;
private string[] cigarettes = new string[] { "Dunhill", "Bleeds", "PallMall", "Prince", "BlueMaster", "庐山", "红双喜", "中华", "利群" };
public cigarette(int num)
{
// TODO: Complete member initialization
this.name = cigarettes[num];
used = false;
}
}
class pet
{
public string name;
public bool used;
private string[] pets = new string[] { "狗", "猫", "鸟", "马", "鱼", "兔", "鼠", "蛇", "蜘蛛" };
public pet(int num)
{
// TODO: Complete member initialization
this.name = pets[num];
used = false;
}
}
#endregion
}