|
|

楼主 |
发表于 2016-1-28 00:05:09
|
显示全部楼层
自问自答。贴代码。不过很奇怪,运行起来很快就出结果。照理说应该枚举一会儿。。不知道哪儿出了问题。下面贴代码。。
- # 分段测试test1、2、3可以提高代码运行效率。
- # 学习了如何用local()设置动态变量名,但并未用到。
- # 如何实现多层循环嵌套并退出循环。。
- # 提前提取题意中的直接信息来简化枚举数据的量
- #用itertools库来穷尽列表的枚举结果。
- import itertools
- import time
- import random
- i = 0
- def test1(A):
- i = A[0].index('red') # 红色房子的id
- i0 = A[4].index('dog') # 养狗的id
- i1 = A[1].index('tea') # 茶的id
- i2 = A[0].index('green') # 绿色房子id
- i10 = A[0].index('white') # 黄色房子
- if A[3][0] != 'Norway':
- print(A[3][0])
- # print('最前面住的不是挪威')
- return 1
- elif A[3][i] != 'UK':
- # print(A[3][i])
- # print('英国人不住在红色房子里。')
- return 2
- elif A[3][i0] != 'Sweden':
- # print('瑞典人不养狗。')
- return 3
- elif A[3][i1] != 'Denmark':
- # print('丹麦人不喝茶。')
- return 4
- elif i2 + 1 != i10:
- # print('绿色的房子不在白色房子的左边。')
- return 5
- elif A[1][i2] != 'coffee':
- # print('绿色房子的主人不喜欢喝咖啡。')
- return 6
- else:
- return 0
- def test2(A):
- i3 = A[0].index('yellow') # 黄色房子
- i4 = A[2].index('坡魔')
- if A[4][i4] != 'bird':
- # print('抽“坡魔”牌雪茄的人不养鸟。')
- return 7
- elif A[2][i3] != '顿山':
- # print('黄色房子的主人不抽“顿山”牌雪茄。')
- return 8
- elif A[1][2] != 'milk':
- # print('谁谁不喝牛奶')
- return 9
- else:
- return 0
- def test3(A):
- i5 = A[4].index('cat') # 养猫的人id
- i6 = A[4].index('horse') # 养马的人id
- i7 = A[1].index('beer') # 喝啤酒id
- i8 = A[3].index('Germany') # 德国人id
- i9 = A[1].index('water')
- if i5 == 0 and A[2][i5 + 1] != '波兰斯':
- # print('抽“波兰斯”牌雪茄的人不住在养猫的人旁边。')
- return 11
- if i5 == 4 and A[2][i5 - 1] != '波兰斯':
- return 12
- elif i5 % 4 != 0 and A[2][i5 + 1] != '波兰斯' and A[2][i5 - 1] != '波兰斯':
- # print('抽“波兰斯”牌雪茄的人不住在养猫的人旁边。')
- return 12
- elif i6 == 0 and A[2][i6 + 1] != '顿山':
- # print('养马的人不住在抽“顿山”牌雪茄的人旁边。')
- return 13
- elif i6 == 4 and A[2][i6 - 1] != '顿山':
- return 14
- elif i6 % 4 != 0 and A[2][i6 + 1] != '顿山' and A[2][i6 - 1] != '顿山':
- # print('养马的人不住在抽“顿山”牌雪茄的人旁边。')
- return 15
- elif A[2][i7] != '蓝领':
- # print('抽“蓝领”牌雪茄的人不喝啤酒。')
- return 16
- elif A[2][i8] != '王子':
- # print('德国人不抽“王子”牌雪茄。')
- return 17
- elif A[0][1] != 'blue':
- # print('挪威人住在蓝色房子旁边。')
- return 18
- elif i9 == 0 and A[2][i9 + 1] != '波兰斯':
- # print('抽“波兰斯”牌雪茄的人有一个喝水的邻居。')
- return 19
- elif i9 == 4 and A[2][i9 - 1] != '波兰斯':
- # print('抽“波兰斯”牌雪茄的人有一个喝水的邻居。')
- return 20
- elif i9 % 4 != 0 and A[2][i9 + 1] != '波兰斯' and A[2][i9 - 1] != '波兰斯':
- # print('抽“波兰斯”牌雪茄的人有一个喝水的邻居。')
- return 21
- else:
- return 0
- colour = ['red', 'yellow', 'green', 'white']
- random.shuffle(colour)
- iter1 = list(itertools.permutations(colour))
- drink = ['water', 'tea', 'coffee', 'beer']
- random.shuffle(drink)
- iter2 = list(itertools.permutations(drink))
- smoke = ['蓝领', '坡魔', '波兰斯', '王子', '顿山']
- random.shuffle(smoke)
- iter3 = list(itertools.permutations(smoke))
- country = ['Denmark', 'Sweden', 'UK', 'Germany']
- random.shuffle(country)
- print(country)
- iter4 = list(itertools.permutations(country))
- pet = ['horse', 'bird', 'cat', 'fish', 'dog']
- random.shuffle(pet)
- print(drink, smoke, pet, country, colour)
- iter5 = list(itertools.permutations(pet))
- i = 0
- def main():
- for j1 in iter1:
- for j2 in iter2:
- for j3 in iter3:
- for j4 in iter4:
- for j5 in iter5:
- global i
- i += 1
- print(i)
- if i % 1000000 == 0 and i <= 20000000:
- print('已检查%d种组合.' % i)
- if i % 1000000 == 0 and 20000000 < i <= 40000000:
- print('已检查%d种组合。你可能运气不佳。平均需要检查1亿种组合,请等待~' % i)
- if i % 1000000 == 0 and 40000000 < i <= 80000000:
- print('已检查%d种组合。坚持,胜利就在眼前~' % i)
- if i % 1000000 == 0 and 80000000 < i <= 160000000:
- print('已检查%d种组合。我已经看到前方的答案了~' % i)
- j22 = list(j2) # 元祖必须转换为list才能进行insert操作。
- j22.insert(2, 'milk')
- j11 = list(j1)
- j11.insert(1, 'blue')
- j44 = list(j4)
- j44.insert(0, 'Norway')
- A = [j11, j22, j3, j44, j5]
- if test1(A) == 0:
- if test2(A) == 0:
- if test3(A) == 0:
- break
- break
- break
- break
- return A, i
- starttime = time.time()
- main()
- endtime = time.time()
- print('共测试了%d个样本,用时%d秒。' % ((main()[1]), endtime - starttime))
- result = main()[0]
- for i in range(1, 6):
- info = result
- print('第%d个房子是%s的,主人是%s人,爱喝%s,爱抽%s烟,养%s。' % (
- i, info[0][i - 1], info[3][i - 1], info[1][i - 1], info[2][i - 1], info[4][i - 1]))
- print('OK,这样一道之前和刁一块儿思考的爱因斯坦五房子题终于用编程的办法解出来啦!不过过程慢哈。anyway,祝生日快乐,学业有成!')
复制代码 |
|