《算法的乐趣》5.三个水桶等分八升水的问题------python

问题描述

有三个容积分别是3升、5升和8升的水桶,其中容积为8升的水桶中装满了谁,容积为3升和5升的水桶是空的。都没有体积刻度,将8升水等分成两份,每份水都是4升水。

问题的关键:

通过倒水凑出确定的1升水或能容纳1升水的空间。

使用穷举法:定义问题的解并分析解空间的范围和拓扑结构,然后根据解空间的范围和拓扑结构实际遍历搜索算法。

状态和动作

三个水桶中存水的情况称为一个状态,初始状态为8升的水桶装满水,其余为空;解的状态为3升桶为空,5升和8升水桶各4升水。
解就是初始状态到最终状态的变化路径。
状态都是静止的,从初始状态到最终状态的变化需要一种推动力,那就是倒水动作。

建模

建立状态和动作的数学模型,并找到一种持续驱动动作产生的搜索方法。
状态的数学模型:本质上对状态的穷举搜索,得到一颗状态搜索树,状态树上的所有状态就构成了穷举算法的解空间。
倒水动作的数学模型:合法的倒水动作。

搜索算法:深度优先搜索,同一时间只需要存储从根节点到当前搜索状态节点这一条路径上的状态节点,需要的存储空间较小。但是要避免重复状态导致的状态环路,防止出现死循环。

递归来实现

from collections import deque

initial_bucket_state = [0, 0, 8]    # 水桶的初始状态
bucket_volume = [3, 5, 8]    # 每个水桶的对应的容积

# 利用python的deque队列记录状态转移情况,初始化时加入水桶初始状态。deque是可以从头尾插入和删除的队列,在不指定大小时,为一个无边界的队列
record = deque()
record.append(initial_bucket_state)

def next_state_lawful(current_state, bucket_volume):
    #通过列表推导式获得下一动作的二元组构成的列表,由(倒出水的容器编号,倒入水的容器编号)组成。
    #二重循环得到下一步的所有可能动作,然后通过1.倒入倒出不能为同一个2.倒出的捅中必须有水3.倒入的桶中不能为满 的条件判断是否合法
    next_action = [(from_, to_) for from_ in range(3) for to_ in range(3) if from_ != to_ and current_state[from_] > 0 and current_state[to_] < bucket_volume[to_] ]
       
    for from_, to_ in next_action:
        #next_state = current_state #浅复制造成错误
        next_state = list(current_state)
        if current_state[from_] + current_state[to_] > bucket_volume[to_]:
            next_state[from_] -= (bucket_volume[to_] - current_state[to_])
            next_state[to_] = bucket_volume[to_]
        else:
            next_state[from_] = 0
            next_state[to_] = current_state[to_] + current_state[from_]    
        
        #再由所有可能的合法动作得出所有的下一个状态,通过yield产生供其它函数调用。
        yield next_state


num = 0
record_list = []
#记录调试的变量:num表示总共实现方法数,record_list记录所有实现路径

def searchResult(record, bucket_volume=[3,5,8], final_bucket_state=[0,4,4]):

    global num, record_list
    #由record的末尾元素得到当前水桶状态
    current_state = record[-1]
    #得到关于当前状态的下一状态的可迭代生成器,供下一步循环使用
    next_state = next_state_lawful(current_state, bucket_volume)

    #遍历所有可能的下一状态
    for state in next_state:
        if state not in record:
            #保证当前状态没在以前出现过。如果状态已经出现还进行搜索就会形成状态环路,陷入死循环。
            record.append(state)
            #添加新的状态到列表中
            if state == final_bucket_state:
                print(record)
                #打印出可行方案
                #record_list.append(record)这样使用错误,导致加入列表的是record的引用,应该使用下面的式子来进行深复制,得到一个新的队列再加入列表。
                record_list.append(deque(record))
                num += 1
            else:
                # 递归搜索
                searchResult(record, bucket_volume, final_bucket_state)
            # 去除当前循环中添加的状态,进入下一个循环,关键步,第一次实现的时候遗漏了   
            record.pop()
searchResult(record)
print("总共实现方式的种类数目:", num)
print("实现方式的最少步骤为:%d 步" % (min([len(i) for i in record_list])-1))
deque([[0, 0, 8], [3, 0, 5], [0, 3, 5], [3, 3, 2], [1, 5, 2], [0, 5, 3], [3, 2, 3], [0, 2, 6], [2, 0, 6], [2, 5, 1], [3, 4, 1], [0, 4, 4]])
deque([[0, 0, 8], [3, 0, 5], [0, 3, 5], [3, 3, 2], [1, 5, 2], [1, 0, 7], [0, 1, 7], [3, 1, 4], [0, 4, 4]])
deque([[0, 0, 8], [3, 0, 5], [0, 3, 5], [3, 3, 2], [1, 5, 2], [1, 0, 7], [0, 1, 7], [3, 1, 4], [3, 5, 0], [0, 5, 3], [3, 2, 3], [0, 2, 6], [2, 0, 6], [2, 5, 1], [3, 4, 1], [0, 4, 4]])
deque([[0, 0, 8], [3, 0, 5], [0, 3, 5], [3, 3, 2], [1, 5, 2], [1, 0, 7], [0, 1, 7], [0, 5, 3], [3, 2, 3], [0, 2, 6], [2, 0, 6], [2, 5, 1], [3, 4, 1], [0, 4, 4]])
deque([[0, 0, 8], [3, 0, 5], [0, 3, 5], [3, 3, 2], [1, 5, 2], [3, 5, 0], [0, 5, 3], [3, 2, 3], [0, 2, 6], [2, 0, 6], [2, 5, 1], [3, 4, 1], [0, 4, 4]])
deque([[0, 0, 8], [3, 0, 5], [0, 3, 5], [3, 3, 2], [3, 5, 0], [0, 5, 3], [3, 2, 3], [0, 2, 6], [2, 0, 6], [2, 5, 1], [3, 4, 1], [0, 4, 4]])
deque([[0, 0, 8], [3, 0, 5], [0, 3, 5], [0, 5, 3], [3, 2, 3], [0, 2, 6], [2, 0, 6], [2, 5, 1], [3, 4, 1], [0, 4, 4]])
deque([[0, 0, 8], [3, 0, 5], [3, 5, 0], [0, 5, 3], [3, 2, 3], [0, 2, 6], [2, 0, 6], [2, 5, 1], [3, 4, 1], [0, 4, 4]])
deque([[0, 0, 8], [0, 5, 3], [3, 2, 3], [0, 2, 6], [2, 0, 6], [3, 0, 5], [0, 3, 5], [3, 3, 2], [1, 5, 2], [1, 0, 7], [0, 1, 7], [3, 1, 4], [0, 4, 4]])
deque([[0, 0, 8], [0, 5, 3], [3, 2, 3], [0, 2, 6], [2, 0, 6], [2, 5, 1], [3, 4, 1], [0, 4, 4]])
deque([[0, 0, 8], [0, 5, 3], [3, 2, 3], [0, 2, 6], [2, 0, 6], [2, 5, 1], [3, 4, 1], [3, 0, 5], [0, 3, 5], [3, 3, 2], [1, 5, 2], [1, 0, 7], [0, 1, 7], [3, 1, 4], [0, 4, 4]])
deque([[0, 0, 8], [0, 5, 3], [3, 2, 3], [0, 2, 6], [2, 0, 6], [2, 5, 1], [3, 4, 1], [3, 5, 0], [3, 0, 5], [0, 3, 5], [3, 3, 2], [1, 5, 2], [1, 0, 7], [0, 1, 7], [3, 1, 4], [0, 4, 4]])
deque([[0, 0, 8], [0, 5, 3], [3, 2, 3], [0, 2, 6], [2, 0, 6], [2, 5, 1], [3, 5, 0], [3, 0, 5], [0, 3, 5], [3, 3, 2], [1, 5, 2], [1, 0, 7], [0, 1, 7], [3, 1, 4], [0, 4, 4]])
deque([[0, 0, 8], [0, 5, 3], [3, 2, 3], [3, 0, 5], [0, 3, 5], [3, 3, 2], [1, 5, 2], [1, 0, 7], [0, 1, 7], [3, 1, 4], [0, 4, 4]])
deque([[0, 0, 8], [0, 5, 3], [3, 2, 3], [3, 5, 0], [3, 0, 5], [0, 3, 5], [3, 3, 2], [1, 5, 2], [1, 0, 7], [0, 1, 7], [3, 1, 4], [0, 4, 4]])
deque([[0, 0, 8], [0, 5, 3], [3, 5, 0], [3, 0, 5], [0, 3, 5], [3, 3, 2], [1, 5, 2], [1, 0, 7], [0, 1, 7], [3, 1, 4], [0, 4, 4]])
总共实现方式的种类数目: 16
实现方式的最少步骤为:7 步
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值