经典逻辑推理

1、有5栋5种颜色的房子。  
2、每一位房子主人的国籍都不同。  
3、5 个人每人只喝一种饮料、只抽一个牌子的香烟、只养一种宠物。  
4、没有有人有相同的宠物、抽相同的香烟、喝相同的饮料。  
提示:  
1、英国人住红房子。  
2、瑞典人养狗。 
3、丹麦人喝茶。  
4、绿房子在白房子的左边 
5、绿房子的主人喝咖啡。  
6、抽PALL MALL烟的人养鸟。  
7、黄房子的主人抽DUNHILL烟。  
8、住中间房子的人喝牛奶。  
9、挪威人住第一间房子。  
10、抽混合烟的人住养猫人旁边。  
11、养马人住抽DNUHILL烟的人旁边。  
12、抽BLUE MASTER烟的人住养马人旁边。 
13、德国人抽DRINCE烟。  
14、挪威人住蓝房子旁边。  
15、抽混合烟的人的邻居喝矿泉水。 


问:谁养鱼?

import math, time


pos = []


nation = ['nuo','eng','rui','dan','gen']  #0
col    = ['blu','red','gre','wri','yel']  #1
drink  = ['tea','bee','mil','cof','wat']  #2
smoke  = ['pal','dun','blu','ble','pri']  #3
pet    = ['dog','cat','bir','fis','hou']  #4


def rul0():
    if pos[0]['nuo'] != 0            : return 0
    return 1


def rul1():
    if pos[0]['eng'] != pos[1]['red']: return 0
    if pos[1]['gre']+1 != pos[1]['wri']: return 0
    if pos[0]['nuo']+1 != pos[1]['blu'] and pos[0]['nuo']-1 != pos[1]['blu'] : return 0
    return 1


def rul2():
    if pos[2]['mil'] != 2            : return 0
    if pos[0]['dan'] != pos[2]['tea']: return 0
    if pos[1]['gre'] != pos[2]['cof']: return 0
    return 1


def rul3():
    if pos[1]['yel'] != pos[3]['dun']: return 0
    if pos[3]['blu'] != pos[2]['bee']: return 0
    if pos[0]['gen'] != pos[3]['pri']: return 0
    if pos[3]['ble']-1 != pos[2]['wat'] and pos[3]['ble']+1 != pos[2]['wat']: return 0
    return 1


def rul4():
    if pos[0]['rui'] != pos[4]['dog']: return 0
    if pos[3]['pal'] != pos[4]['bir']: return 0
    if pos[3]['ble']-1 != pos[4]['cat'] and pos[3]['ble']+1 != pos[4]['cat']: return 0
    if pos[4]['hou']-1 != pos[3]['dun'] and pos[4]['hou']+1 != pos[3]['dun'] : return 0
    return 1


def toarrge(val, lis, res):
    tmp = lis[:]
    for j in xrange(0,5):
        i = val%(5-j)
        val /= 5-j
        ts = tmp[j]
        tmp[j] = tmp[i+j]
        tmp[i+j] = ts
    for j in xrange(0,5):
        res[tmp[j]] = j


pos = [{}, {}, {}, {}, {} ]
tic = time.time()
for i0 in xrange(0, 120):
    res = {}
    toarrge(i0, nation, res) 
    pos[0] = res
    if rul0() != 1: continue
    for i1 in xrange(0, 120):
        res = {}
        toarrge(i1, col, res) 
        pos[1] = res
        if rul1() != 1: continue
        for i2 in xrange(0, 120):
            res = {}
            toarrge(i2, drink, res) 
            pos[2] = res
            if rul2() != 1: continue
            for i3 in xrange(0, 120):
                res = {}
                toarrge(i3, smoke, res) 
                pos[3] = res
                if rul3() != 1: continue
                for i4 in xrange(0, 120):
                    res = {}
                    toarrge(i4, pet, res) 
                    pos[4] = res
                    if rul4() ==  1:
                        print "got it!! cost time %f sencond.\n"%(time.time()-tic), pos
                        for item in pos[0].iteritems():
                            if item[1] == pos[4]['fis']:
                                print "the lord of fish is", item[0]
                        exit(0)


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值