■在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.
(待续)