遇到百思不得其解的 bug

2015-10-05 02:11:19 +08:00
 lbfeng

代码在此: http://7xn6e9.com1.z0.glb.clouddn.com/ai.txt
生成 5*5 的数次迷宫,从左上角跳到右下角,数字表示要跳的格数,我用广度优先可以算出结果,现在想用 uniform cost, 总是出现 IndexError : index out of range 。
运行结果:

Rook Jumping Maze size (5-10)?5
[4, 2, 2, 1, 4]
[3, 3, 2, 2, 2]
[4, 1, 2, 1, 1]
[1, 1, 1, 3, 1]
[4, 1, 1, 3, 0]
explore node
(0, 0)
action S
explore child
(0, 0) (4, 0) # 第一个是 parent, 第二个是 child
push it in the frontier
action E
** # parent 的坐标
0 0
**
explore child
(0, 0) (0, 4)
push it in the frontier
explore node
(4, 0)
action N
explore child
(4, 0) (0, 0)
action E
**
0 4
**
explore child # parent 突然变了??
(0, 4) (0, 8) # child node 出错了
push it in the frontier
explore node
(0, 4)
action S
explore child
(0, 4) (4, 4)
push it in the frontier

2916 次点击
所在节点    Python
6 条回复
yxwzaxns
2015-10-05 06:39:13 +08:00
题目太长不看
lbfeng
2015-10-05 07:44:50 +08:00
@yxwzaxns

for child_node in expend(RJM, node, action):
child_node.distance = node.distance + 1
child_node.parent = node
print "explore child"
print child_node.parent, child_node
if not ((child_node in [node for distance, node in frontier]) or (child_node in explored)):
heappush(frontier, (child_node.distance,child_node))
print "push it in the frontier"
else:
....

我已经解决了。
“[node for distance, node in frontier]” 这里的 node 影响了 for 循环里的 node “ expend(RJM, node, action):”
为什么呢?
mulog
2015-10-05 08:26:16 +08:00
因为 list comprehension 没有自己的 namespace, 注意不要和外面的变量重名
aec4d
2015-10-05 09:47:22 +08:00
@mulog python3 列表推导式有自己的命名空间 python2 中可以用 map 也有自己的命名空间
mulog
2015-10-05 09:54:30 +08:00
@aec4d 是,窝不严谨了 :p
21grams
2015-10-05 12:57:31 +08:00
TL;DR

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/225652

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX