我在广度搜索计算推箱子的主要代码部分如下:def findbest(levelObj,gameStateObj,mapObj):
goallst=levelObj['goals'].copy()
goallst.sort()
boxlst=gameStateObj['stars'].copy()
boxlst.sort()
playerxy = gameStateObj['player']
a=[]
a.append(boxlst)
a.append(playerxy)
ss=[]
ss.append([a])
#print(ss)
#return
while ss:
path = ss.pop(0)
node = path[-1]
#print(path)
#print(node,node[1])
#return
if node[0] == goallst:
print(path)
#print(path[0])
print(path[-1])
return path.reverse()
px,py=node[1]
bfs_add_node(mapObj,ss,path,node[0],px,py,1,0)
#print(ss)
bfs_add_node(mapObj,ss,path,node[0],px,py,0,1)
#print(ss)
bfs_add_node(mapObj,ss,path,node[0],px,py,-1,0)
#print(ss)
bfs_add_node(mapObj,ss,path,node[0],px,py,0,-1)
#print(ss)
#return
return []
def bfs_add_node(mapObj,ss,path_,boxlst,px,py,offx,offy):
path=path_.copy()
if isNotBlocked(mapObj,boxlst , px+offx, py+offy):
temlst=[]
temlst.append(boxlst)
temlst.append((px+offx, py+offy))
#print(temlst)
if not temlst in path:
"""
tt=[]
tt.append(path)
tt.append(temlst)
ss.append(tt)
"""
ss.append([path,temlst])
elif (px+offx, py+offy) in boxlst:
if isNotBlocked(mapObj,boxlst , px+2*offx, py+2*offy):
temboxlst=boxlst.copy()
ind = temboxlst.index((px+offx, py+offy))
temboxlst[ind] = (px+2*offx, py+2*offy)
temboxlst.sort()
temlst=[]
temlst.append(temboxlst)
temlst.append((px+offx, py+offy))
#print(temlst)
#print(path)
if not temlst in path:
"""
tt=[]
tt.append(path)
tt.append(temlst)
ss.append(tt)
"""
ss.append([path,temlst])
第一关调用函数返回的是
[[[[[[[[[(3, 4), (4, 3), (4, 5), (5, 4)], (4, 4)]], [[(3, 4), (4, 3), (4, 5), (6
, 4)], (5, 4)]], [[(3, 4), (4, 3), (4, 5), (6, 4)], (4, 4)]], [[(3, 4), (4, 3),
(4, 6), (6, 4)], (4, 5)]], [[(3, 4), (4, 3), (4, 6), (6, 4)], (4, 4)]], [[(2, 4)
, (4, 3), (4, 6), (6, 4)], (3, 4)]], [[(2, 4), (4, 3), (4, 6), (6, 4)], (4, 4)]]
我期待它返回的代码结果是
[[[(3, 4), (4, 3), (4, 5), (5, 4)], (4, 4)], [[(3, 4), (4, 3), (4, 5), (6
, 4)], (5, 4)], [[(3, 4), (4, 3), (4, 5), (6, 4)], (4, 4)], [[(3, 4), (4, 3),
(4, 6), (6, 4)], (4, 5)], [[(3, 4), (4, 3), (4, 6), (6, 4)], (4, 4)], [[(2, 4)
, (4, 3), (4, 6), (6, 4)], (3, 4)], [[(2, 4), (4, 3), (4, 6), (6, 4)], (4, 4)]]
原因何在?
请高人指点
|