666
学习!感谢!
aaaaaaaaaaaaaa
我只想看答案
看答案
state0 = #勺子的初始状态
maxv = #每个勺子的对应的容积
def nextstate(state0): #对于状态state0,推出所有可能的下一状态
a,b=state0,state0
if a>0: #倒空某个勺子
yield
if b>0:
yield
if a<maxv:
yield ,b]#从酒缸里装满某个勺子
tmp=maxv-a #差值
if b>=tmp:
yield ,b-tmp]
else:
yield
if b<maxv:
yield ]
tmp=maxv-b #差值
if a>=tmp:
yield ]
else:
yield
n=0
from collections import deque
res =[]
def findway(ways):
global n#多少种方法
for state in nextstate(ways[-1]): #遍历下一状态(ways[-1]表示最后一个状态)
if state not in ways: #当前状态未出现过。
ways.append(state) #添加新结果
if state == 2 or state==2:
n+=1
print('第%d种,共%d步:' %(n,len(ways)),ways) #输出结果
res.append(deque(ways))
else:
findway(ways)#递归搜索
ways.pop() #去除当前循环中添加的状态(回归上一级,搜索另一支线。)
findway()
bestway=sorted(res,key=lambda x:len(x))
print("最佳路径(共"+str(len(bestway))+"步):")
for x in bestway:
print(x)
改一下,不超过30行代码。。。。。。
state0 = #勺子的初始状态
maxv = #每个勺子的对应的容积
def nextstate(state0): #对于状态state0,推出所有可能的下一状态
a,b=state0,state0
res=[(0,b),(a,0),(maxv,b),(a,maxv)] #清空,加满
delta=maxv-a if b>=maxv-a else b #b-->a
res.append((a+delta,b-delta))
delta=maxv-b if a>=maxv-b else a #a-->b
res.append((a-delta,b+delta))
res=list(set(res))
if (a,b) in res:
res.remove((a,b))
return res
from collections import deque
res =[]
def findway(ways):
for state in nextstate(ways[-1]): #遍历下一状态(ways[-1]表示最后一个状态)
if state not in ways: #当前状态未出现过。
ways.append(state) #添加新结果
if state == 2 or state==2:
res.append(deque(ways))
else:
findway(ways)#递归搜索
ways.pop() #去除当前循环中添加的状态(回归上一级,搜索另一支线。)
findway()
bestway=sorted(res,key=lambda x:len(x))
print("最佳路径(共"+str(len(bestway))+"步):",)
学习虚席
WOZHISHIWEILEANDAN
不懂得东西,来学习了
算法不会
kankan
我只是为了看答案
探究下代码中的秘密
nihao
帅
看看答案,最近有点不太会
da
标记
KKK