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