考虑 A* 算法。
在 Google 中可以找到一个很好的伪代码:
function A*(start,goal)
closedset := the empty set // The set of nodes already evaluated.
openset := set containing the initial node // The set of tentative nodes to be evaluated.
came_from := the empty map // The map of navigated nodes.
g_score[start] := 0 // Distance from start along optimal path.
h_score[start] := heuristic_estimate_of_distance(start, goal)
f_score[start] := h_score[start] // Estimated total distance from start to goal through y.
while openset is not empty
x := the node in openset having the lowest f_score[] value
if x = goal
return reconstruct_path(came_from, came_from[goal])
remove x from openset
add x to closedset
foreach y in neighbor_nodes(x)
if y in closedset
continue
tentative_g_score := g_score[x] + dist_between(x,y)
if y not in openset
add y to openset
tentative_is_better := true
elseif tentative_g_score < g_score[y]
tentative_is_better := true
else
tentative_is_better := false
if tentative_is_better = true
came_from[y] := x
g_score[y] := tentative_g_score
h_score[y] := heuristic_estimate_of_distance(y, goal)
f_score[y] := g_score[y] + h_score[y]
Update(closedset,y)
Update(openset,y)
return failure
function reconstruct_path(came_from, current_node)
if came_from[current_node] is set
p = reconstruct_path(came_from, came_from[current_node])
return (p + current_node)
else
return current_node
好吧,我不明白一件事:考虑一下图中的情况:
A* 如何能够从 a->b->c 变为 a->d... ??? 好吧,我的意思是,A* 从一个节点开始并通过节点导航。在某一点,一个节点有多个邻居,嗯,A* 能够遵循由邻居生成的路径,但在某一点它能够切换......并从前一个开始返回它的步骤节点并走另一条路,即使废弃的路径没有穿过那条路......
在代码中,启用此环境的条件是什么?