A *搜索算法
(来自: https://en.wikipedia.org/wiki/A*_search_algorithm)
下面的伪代码描述了算法[可疑 - 讨论]:
function A*(start,goal)
ClosedSet := {} // The set of nodes already evaluated.
OpenSet := {start} // The set of tentative nodes to be evaluated, initially containing the start node
Came_From := the empty map // The map of navigated nodes.
g_score := map with default value of Infinity
g_score[start] := 0 // Cost from start along best known path.
// Estimated total cost from start to goal through y.
f_score := map with default value of Infinity
f_score[start] := g_score[start] + heuristic_cost_estimate(start, goal)
while OpenSet is not empty
current := the node in OpenSet having the lowest f_score[] value
if current = goal
return reconstruct_path(Came_From, goal)
OpenSet.Remove(current)
ClosedSet.Add(current)
for each neighbor of current
if neighbor in ClosedSet
continue // Ignore the neighbor which is already evaluated.
tentative_g_score := g_score[current] + dist_between(current,neighbor) // length of this path.
if neighbor not in OpenSet // Discover a new node
OpenSet.Add(neighbor)
else if tentative_g_score >= g_score[neighbor]
continue // This is not a better path.
// This path is the best until now. Record it!
Came_From[neighbor] := current
g_score[neighbor] := tentative_g_score
f_score[neighbor] := g_score[neighbor] + heuristic_cost_estimate(neighbor, goal)
return failure
function reconstruct_path(Came_From,current)
total_path := [current]
while current in Came_From.Keys:
current := Came_From[current]
total_path.append(current)
return total_path
所以,只要我明白 - 你可以设置你的开始节点在红点位置\中心位置,目标节点为x = 0或y = 0或x = 8或y = 8(可以进行4次函数调用,并取最小值)
至于节点的启发式值 - 只需设置黑色阻塞节点非常高的启发式值,这将使它们无法访问,所以算法会绕过它们。
什么构成“路径?”你能举一些例子吗? Upvote包括一个漂亮的图片的问题。 –
这是什么意思?目的地 - 矩阵的边界[意思是x = 0或y = 0或x = 9或y = 9] 9次成功移动? –
请参阅编辑中的示例。红点必须达到迷宫的边界。 – jpm