■在A *方法總結node
Summary of the A* Methodweb
好了,現在你通過解釋已經走了,讓我們奠基了一步一步的方法,在同一個地方:算法
Okay, now that you have gone through the explanation, let's lay out the step-by-step method all in one place:ide
添加開始方塊(或節點)到開啓列表。this
Add the starting square (or node) to the open list.spa
重複如下操做:code
Repeat the following:orm
a) 尋找開啓列表上最小F值的方塊。我們將此做爲當前方塊。排序
Look for the lowest F cost square on the open list. We refer to this as the current squareci
b) 切換到關閉列表。
Switch it to the closed list.
c) 對於當前方塊的8個方塊的每一個...
c) For each of the 8 squares adjacent to this current square …
若是不能走,或者若是它是關閉的名單上,忽略它。否則,請執行如下操做。
If it is not walkable or if it is on the closed list, ignore it. Otherwise do the following.
若是不在開啓列表中,將其添加到開啓列表。使當前方塊成爲這個方塊的父。記錄的方塊F值,G值和H值。
If it isn't on the open list, add it to the open list. Make the current square the parent of this square. Record the F, G, and H costs of the square.
若是在開啓列表了,檢查,看看這個路徑,該方塊是否是更好的,採用G值做爲衡量。更低的G值意味着這是一個更好的路徑。若是是這樣,把方格的父改變當前方塊,並從新計算方塊的G值和F值。若是你保持開啓列表排序F值,因爲這個變化你可能需重存列表。
If it is on the open list already, check to see if this path to that square is better, using G cost as the measure. A lower G cost means that this is a better path. If so, change the parent of the square to the current square, and recalculate the G and F scores of the square. If you are keeping your open list sorted by F score, you may need to resort the list to account for the change.
d)當你中止:
d) Stop when you:
目標方塊添加到關閉列表,在這種狀況下,路徑已經被發現(見下面的注),或沒法找到目標方塊,而且開啓列表是空的。在這種狀況下,不存在路徑。
Add the target square to the closed list, in which case the path has been found (see note below), or Fail to find the target square, and the open list is empty. In this case, there is no path.
保存路徑。從目標方塊往回走,從每一個方塊移到其父,直到你到達開始方塊。這是你的路徑。
Save the path. Working backwards from the target square, go from each square to its parent square until you reach the starting square. That is your path.
注:在早期版本的文章中,有人建議,當目標方塊(或節點)已經添加到開啓列表,而不是關閉的列表,你能夠停下來。這樣作會更快,它幾乎總是會給你的最短路徑,但並不是總是如此。有些狀況下,這樣作可能產生差別當從第二移動到最後一個節點到最後的(目標)節點的運動成本可能有明顯變化 -例如,如果在河流交叉在兩個節點之間的狀況下。
Note: In earlier versions of this article, it was suggested that you can stop when the target square (or node) has been added to the open list, rather than the closed list. Doing this will be faster and it will almost always give you the shortest path, but not always. Situations where doing this could make a difference are when the movement cost to move from the second to the last node to the last (target) node can vary significantly -- as in the case of a river crossing between two nodes, for example.
■小咆哮
Small Rant
請原諒個人題外話,但值得指出的是,當你在網上閱讀的A *路徑搜索,並在各類論壇上的各類討論時,你偶爾會看到有人提到某些代碼不是A *。對於A *使用方法,你須要包含上面討論到的元素 -- 特別是開放列表和關閉列表和路徑採用F值,G值和H值。有不少其餘的路徑搜索算法,可是其它的一般被認爲是最好的方法不是A *。在這篇文章的末尾有布萊恩斯托特討論,包括他們的一些利弊引用的文章不少。有時替代品在某些狀況下更好,但你應該明白你正在進入。好了,爽了。回到話題。
Forgive me for digressing, but it is worth pointing out that when you read various discussions of A* pathfinding on the web and in assorted forums, you will occasionally see someone refer to certain code as A* when it isn't. For the A* method to be used, you need to include the elements just discussed above -- specifically open and closed lists and path scoring using F, G, and H. There are lots of other pathfinding algorithms, but those other methods are not A*, which is generally considered to be the best of the lot. Bryan Stout discusses many of them in the article referenced at the end of this article, including some of their pros and cons. Sometimes alternatives are better under certain circumstances, but you should understand what you are getting into. Okay, enough ranting. Back to the article.
(待續)