wc = ['wolf','sheep','ve'] #西岸初始状态
ec = ['None'] #东岸初始状态
result = [] #记录成功方案的轨迹
table = [] #搜索过程的备忘录
farmerloc = 'wc' #农夫的位置
res = 0 #成功方案个数
def backtrack(wc, ec, farmerloc, lastdelivery, table): #lastdelivery是上次运输的东西,
global res
for i in result: #检查该状态是否在已成功的案例中出现过
if [wc,ec,farmerloc,lastdelivery] in i:
return
if len(ec) == 4: #如果东岸列表已有4个(包括'None')元素则结束
res += 1
result.append(table)
return
#检查非法状态
if 'wolf' in wc and 'sheep' in wc and farmerloc == 'ec':
return
elif 'wolf' in ec and 'sheep' in ec and farmerloc == 'wc':
return
elif 'sheep' in wc and 've' in wc and farmerloc == 'ec':
return
elif 'sheep' in ec and 've' in ec and farmerloc == 'wc':
return
#回溯过程
if farmerloc == 'wc':
for i in wc[:]:
if i == lastdelivery:
continue
table.append([wc, ec, farmerloc, i])
wc.remove(i)
ec.append(i)
farmerloc = 'ec'
backtrack(wc[:], ec[:], farmerloc, i, table[:])
wc.append(i)
ec.remove(i)
farmerloc = 'wc'
if farmerloc == 'ec':
for i in ec[:]:
if i == lastdelivery:
continue
if i == 'None':
table.append([wc, ec, farmerloc, i])
farmerloc = 'wc'
backtrack(wc[:], ec[:], farmerloc, i, table[:])
farmerloc = 'ec'
else:
table.append([wc, ec, farmerloc, i])
ec.remove(i)
wc.append(i)
farmerloc = 'wc'
backtrack(wc[:], ec[:], farmerloc, i, table[:])
ec.append(i)
wc.remove(i)
farmerloc = 'ec'
backtrack(wc, ec, farmerloc, 'None', table[:])
print(res)
农夫过河 python 回溯
于 2023-02-03 20:41:10 首次发布