问题:
- 有5个具有不同颜色的房间;
- 每个房间里住着一个不同国籍的人;
- 每个人都在喝一种特定品牌的饮料;
- 抽一特定品牌的香烟;
- 养一特定的宠物;
- 没有任意的两个人在抽相同品牌的烟、或喝相同品牌饮料、或养相同的宠物。
- 同时提供下面这些线索:
- 英国人住在红房子里
- 瑞典人养狗
- 丹麦人喝茶
- 绿房子紧挨着白房子,在白房子的左边
- 绿房子主人喝咖啡
- 抽PALL MALL牌香烟的人养鸟
- 黄房子里的人抽DUNHILL牌的烟
- 住中间房子的人喝牛奶
- 挪威人住在第一个房子里(最左边)
- 抽BLENDS香烟的人和养猫的人相邻
- 养马的人和抽DUNHILL牌香烟的人相邻
- 抽BLUEMASTER牌香烟的人喝啤酒
- 德国人抽PRINCE牌香烟
- 挪威人和住蓝房子的人相邻
- 抽BLENDS牌香烟的人和喝矿泉水的人相邻。
- 问:谁在养鱼?
Solution:
# -*- coding: GBK -*-
import time
import itertools
def handle(data):
# 英国人住红色房子
index = data[0].index('红色')
if data[1][index] !='英国':
return -1
# 瑞典人养狗
index = data[4].index('狗')
if data[1][index] !='瑞典':
return -2
# 丹麦人喝茶
index = data[2].index('茶')
if data[1][index] !='丹麦':
return -3
# 抽Pall Mall香烟的人养鸟
index = data[3].index('Pall Mall')
if data[4][index] != '鸟':
return -6
# 黄色房子主人抽Dunhill香烟
index = data[3].index('Dunhill')
if data[0][index] != '黄色':
return -7
# 抽Blue Master的人喝啤酒
index = data[3].index('BlueMaster')
if data[2][index] != '啤酒':
return -12
# 德国人抽Prince香烟
index = data[3].index('Prince')
if data[1][index] != '德国':
return -13
# 绿色房子在白色房子左面
index = data[0].index('绿色')
if index == 4: # 绿色房子在最后
return -4
if data[0][index + 1] != '白色':
return -4
# 绿色房子主人喝咖啡
if data[2][index] != '咖啡':
return -5
# 抽Blends香烟的人住在养猫的人隔壁
index = data[3].index('Blends')
cat_index = data[4].index('猫')
if cat_index - index != 1 and cat_index - index != -1:
return -10
# 养马的人住抽Dunhill香烟的人隔壁
index = data[3].index('Dunhill')
horse_index = data[4].index('马')
if horse_index - index != 1 and horse_index - index != -1:
return -11
# 抽Blends香烟的人有一个喝水的邻居
index = data[3].index('Blends')
water_index = data[2].index('水')
if water_index - index != 1 and water_index - index != -1:
return -15
print('找到答案:')
for d_item in data:
print(d_item)
return 0
colour_list_ = list(itertools.permutations(['红色', '黄色', '蓝色', '白色', '绿色'], 5))
country_list_ = list(itertools.permutations(['英国', '丹麦', '挪威', '德国', '瑞典'], 5))
drinks_list_ = list(itertools.permutations(['茶', '水', '咖啡', '啤酒', '牛奶'], 5))
smoke_list_ = list(itertools.permutations(['Pall Mall', 'Dunhill', 'BlueMaster', 'Blends', 'Prince'], 5))
pet_list_ = list(itertools.permutations(['猫', '马', '鱼', '鸟', '狗'], 5))
colour_list = []
country_list = []
drinks_list = []
smoke_list = []
pet_list = []
for colour in colour_list_:
colour = list(colour)
if colour[1] == '蓝色':
colour_list.append(colour)
for country in country_list_:
country = list(country)
if country[0] == '挪威':
country_list.append(country)
for drinks in drinks_list_:
drinks = list(drinks)
if drinks[2] == '牛奶':
drinks_list.append(drinks)
for i in range(120):
smoke_list.append(list(smoke_list_[i]))
for i in range(120):
pet_list.append(list(pet_list_[i]))
# ~ num = 0
start = time.time()
for country in country_list:
for drinks in drinks_list:
for smoke in smoke_list:
for pet in pet_list:
for colour in colour_list:
data_list = [colour, country, drinks, smoke, pet]
# ~ num += 1
if handle(data_list) == 0:
# ~ print("总运行次数 %d" % num)
exit()
end = time.time()
print("计算用时:%0.4f秒" % (end - start))
Output: