一道面试算法题

最近面微信让做这道题

  • 题目:
    给一个装满水的 8 升满壶和两个分别是 5 升、3 升的空壶,想个办法,使得其中一个水壶恰好装 4 升水,每一步的操作只能是倒空或倒满

  • 思路:
    遍历+状态记忆

最终结果不是最优的。。问题应该是返回条件不太对,但是没想到怎么改,先放出来吧

class State:
     def __init__(self,a,b,c):
          self.a = a
          self.b = b
          self.c = c

s1 = State(8,0,0)
A,B,C = 8,5,3
memo = [ ]

action = {
        0:'将杯子A的水倒完给B',
        1:'将杯子A的水倒满给B',
        2:'将杯子A的水倒完给C',
        3:'将杯子A的水倒满给C',
        4:'将杯子B的水倒完给A',
        5:'将杯子B的水倒满给A',
        6:'将杯子B的水倒完给C',
        7:'将杯子B的水倒满给C',
        8:'将杯子C的水倒完给A',
        9:'将杯子C的水倒满给A',
        10:'将杯子C的水倒完给B',
        11:'将杯子C的水倒满给B'
        }

def bfs(state,line):
    if state.a == target or state.b == target or state.c == target:
        print(line)
        return
    for i in range(len(action)):
        if (state.a,state.b,state.c,i) in memo:
            continue
        memo.append((state.a,state.b,state.c,i))
        if i == 0:
            if state.a == 0:
                continue
            if state.a + state.b < B:
                line.append(action[i])
                state_next = State(0,state.b+state.a,state.c)
                bfs(state_next,line)
                line.pop(-1)
        if i == 1:
            if state.a == 0:
                continue
            if state.a + state.b >= B and state.b != B:
                line.append(action[i])
                state_next = State(state.a - (B-state.b),B,state.c)
                bfs(state_next,line)
                line.pop(-1)
        if i == 2:
            if state.a == 0:
                continue
            if state.a + state.c < C:
                line.append(action[i])
                state_next = State(0,state.b,state.c+state.a)
                bfs(state_next,line)
                line.pop(-1)
        if i == 3:
            if state.a == 0:
                continue
            if state.a + state.c >= C and state.c != C:
                line.append(action[i])
                state_next = State(state.a - (C-state.c),state.b,C)
                bfs(state_next,line)
                line.pop(-1)
        if i == 4:
            if state.b == 0:
                continue
            if state.b + state.a < A:
                line.append(action[i])
                state_next = State(state.a+state.b,0,state.c)
                bfs(state_next,line)
                line.pop(-1)
        if i == 5:
            if state.b == 0:
                continue
            if state.a + state.b >= A and state.a != A:
                line.append(action[i])
                state_next = State(A,state.b - (A-state.a),state.c)
                bfs(state_next,line)
                line.pop(-1)
        if i == 6:
            if state.b == 0:
                continue
            if state.b + state.c < C:
                line.append(action[i])
                state_next = State(state.a,0,state.c+state.b)
                bfs(state_next,line)
                line.pop(-1)
        if i == 7:
            if state.b == 0:
                continue
            if state.b + state.c >= C and state.c != C:
                line.append(action[i])
                state_next = State(state.a,state.b - (C-state.c),C)
                bfs(state_next,line)
                line.pop(-1)
        if i == 8:
            if state.c == 0:
                continue
            if state.a + state.c < A:
                line.append(action[i])
                state_next = State(state.a+state.c,state.b,0)
                bfs(state_next,line)
                line.pop(-1)
        if i == 9:
            if state.c == 0:
                continue
            if state.a + state.c >= A and state.a != A:
                line.append(action[i])
                state_next = State(A,state.b,state.c - (A-state.a))
                bfs(state_next,line)
                line.pop(-1)
        if i == 10:
            if state.c == 0:
                continue
            if state.b + state.c < B:
                line.append(action[i])
                state_next = State(state.a,state.b+state.c,0)
                bfs(state_next,line)
                line.pop(-1)
        if i == 11:
            if state.c == 0:
                continue
            if state.b + state.c >= B and state.b != B:
                line.append(action[i])
                state_next = State(state.a,B,state.c - (B-state.b))
                bfs(state_next,line)
                line.pop(-1)
target = 4
bfs(s1,[ ])



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值