代码在此: 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
1
yxwzaxns 2015-10-05 06:39:13 +08:00 via Android
题目太长不看
|
2
lbfeng OP @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):” 为什么呢? |
3
mulog 2015-10-05 08:26:16 +08:00
因为 list comprehension 没有自己的 namespace, 注意不要和外面的变量重名
|
6
21grams 2015-10-05 12:57:31 +08:00
TL;DR
|