用两个量筒得出一定加仑水的问题

来源:互联网 发布:开源微商城源码 编辑:程序博客网 时间:2024/05/16 19:07
#一个a加仑的量筒,一个b加仑量筒,得到c加仑水
class node(object):    def __init__(self):        self.cura = 0        self.curb = 0        self.x = []        self.pairs = [[0,0]]def append_node(wlist,tempa,tempb,i,old):    new_node = node()    new_node.cura = tempa    new_node.curb = tempb    new_node.x = old.x[:]    new_node.x.append(i)    new_node.pairs = old.pairs[:]    new_node.pairs.append([tempa,tempb])    wlist.append(new_node)def water(a,b,c):    root = node()    wlist = []    wlist.append(root)    while(len(wlist) > 0):        old = wlist.pop(0)        #print old.pairs        if old.cura==c or old.curb==c:            print old.pairs            return        i = 1        while i <= 5:            if i == 1:                tempa = a                tempb = old.curb                if [tempa,tempb] in old.pairs:                    i += 1                    continue                append_node(wlist,tempa,tempb,i,old)            if i == 2:                tempa = 0                tempb = old.curb                if [tempa,tempb] in old.pairs:                    i += 1                    continue                append_node(wlist,tempa,tempb,i,old)            if i == 3:                tempa = old.cura                tempb = b                if [tempa,tempb] in old.pairs:                    i += 1                    continue                append_node(wlist,tempa,tempb,i,old)            if i == 4:                tempa = old.cura                tempb = 0                if [tempa,tempb] in old.pairs:                    i += 1                    continue                append_node(wlist,tempa,tempb,i,old)            if i == 5:                left_b = b - old.curb                if old.cura <= left_b:                    tempa = 0                    tempb = old.curb + old.cura                else:                    tempa = old.cura - left_b                    tempb = b                if [tempa,tempb] in old.pairs:                    i += 1                    continue                append_node(wlist,tempa,tempb,i,old)            if i == 6:                left_a = a - old.cura                if old.curb <= left_a:                    tempa = old.cura +old.curb                    tempb = 0                else:                    tempa = a                    tempb = old.curb -left_a                if [tempa,tempb] in old.pairs:                    i += 1                    continue                append_node(wlist,tempa,tempb,i,old)            i += 1                                            

0 0