■繼續搜索node
Continuing the Searchapp
要繼續搜索,我們簡單地選擇在開啓列表中具備最小F值的方塊。而後,我們用選擇方塊做如下事情:ui
To continue the search, we simply choose the lowest F score square from all those that are on the open list. We then do the following with the selected square:this
1.把它從開啓列表取出,並加入到關閉列表。spa
1.Drop it from the open list and add it to the closed list.rest
2.檢查全部的相鄰方塊。忽略那些在關閉列表裏或不可行走的(牆,水或其餘非法地形),若是它們還不在開啓列表中,加方塊到開啓列表。將選定方塊做爲新方塊的「父」。three
2.Check all of the adjacent squares. Ignoring those that are on the closed list or unwalkable (terrain with walls, water, or other illegal terrain), add squares to the open list if they are not on the open list already. Make the selected square the "parent" of the new squares.get
3.若是相鄰的方塊已經在開啓列表,查看這條路徑到那個方塊是不是一個更好的。換句話說,檢查,看看方塊的G值是不是較低,若是我們使用當前方塊到那裏。若是沒有,什麼也不作。it
3.If an adjacent square is already on the open list, check to see if this path to that square is a better one. In other words, check to see if the G score for that square is lower if we use the current square to get there. If not, don't do anything.io
另外一方面,若是新路徑的G值較低,改變相鄰方格的父到選定方格(圖中上方,改變指針的方向指向在所選擇的方格)。最後,從新計算那個方塊的兩個F和G的值。若是這彷佛使人困惑,你會看到它所示。
On the other hand, if the G cost of the new path is lower, change the parent of the adjacent square to the selected square (in the diagram above, change the direction of the pointer to point at the selected square). Finally, recalculate both the F and G scores of that square. If this seems confusing, you will see it illustrated below.
好吧,看看這是如何工做的。我們最初的9個方格,在起始方塊被放到關閉列表後還有8個在開啓列表。其中,具備最低F值的是一個起點方塊緊鄰右側的,其F值爲40。所以,我們選擇這個方塊做爲我們的下一個方塊。以下圖所示高亮的藍色的部分。
Okay, so let's see how this works. Of our initial 9 squares, we have 8 left on the open list after the starting square was switched to the closed list. Of these, the one with the lowest F cost is the one to the immediate right of the starting square, with an F score of 40. So we select this square as our next square. It is highlight in blue in the following illustration.
[圖4]
[Figure 4]
首先,從開啓列表中刪除它,並把它添加到關閉列表(這就是它以高亮藍色顯示的緣由)。而後檢查相鄰的方塊。好了,這個方塊的相鄰右邊的是牆上的方塊,忽略它。一個眼前的左邊是開始方塊。這是關閉的名單上,因此也忽略它。
First, we drop it from our open list and add it to our closed list (that's why it's now highlighted in blue). Then we check the adjacent squares. Well, the ones to the immediate right of this square are wall squares, so we ignore those. The one to the immediate left is the starting square. That's on the closed list, so we ignore that, too.
其餘四個方格已經在開啓列表中,因此我們須要檢查若是使用那些方塊做爲路徑是否比使用這個方塊到那裏更好,將G值做爲我們的參照點。來看看選擇的這個方塊吧。它的G值是14,若是經由當前方塊到達那裏,G值將等於20(10,如今方塊的G值,再加上10來垂直移到它的上面)。G值20比14高,因此這不是一個更好的路徑。若是你看一下圖能更好的理解這些。從開始方格方塊沿對角線移動一個方格到那裏更直接些,而不是水平移動一個方塊,再垂直移動一個方塊。
The other four squares are already on the open list, so we need to check if the paths to those squares are any better using this square to get there, using G scores as our point of reference. Let's look at the square right above our selected square. Its current G score is 14. If we instead went through the current square to get there, the G score would be equal to 20 (10, which is the G score to get to the current square, plus 10 more to go vertically to the one just above it). A G score of 20 is higher than 14, so this is not a better path. That should make sense if you look at the diagram. It's more direct to get to that square from the starting square by simply moving one square diagonally to get there, rather than moving horizontally one square, and then vertically one square.
當對在開啓列表中的4個相鄰方格重複做這個過程,我們發現沒有路徑比當前的方塊有提升,所以我們不會改變任何東西。因此,現在,我們看了全部的相鄰方塊,我們都與這個方塊做完畢,並準備移動到下一個方塊。
When we repeat this process for all 4 of the adjacent squares already on the open list, we find that none of the paths are improved by going through the current square, so we don't change anything. So now that we looked at all of the adjacent squares, we are done with this square, and ready to move to the next square.
那麼,通過開啓列表,現在減到7個方格的列表,我們選擇了一個具備最小F值。有趣的是,在這種狀況下,有兩個方塊的F值是54,那麼我們選擇哪一個?這其實並不重要。爲了快速的目的,它能夠更快地選擇您添加到開啓列表中最後一個。這種偏向獲得後來發現在搜索時,當你更接近目標時。但它其實並不重要。 (不一樣的處理形成了兩個版本的A *可能找到不一樣的等長路徑。)
So we go through the list of squares on our open list, which is now down to 7 squares, and we pick the one with the lowest F cost. Interestingly, in this case, there are two squares with a score of 54. So which do we choose? It doesn't really matter. For the purposes of speed, it can be faster to choose the last one you added to the open list. This biases the search in favor of squares that get found later on in the search, when you have gotten closer to the target. But it doesn't really matter. (Differing treatment of ties is why two versions of A* may find different paths of equal length.)
所以,讓我們選擇一個下方,並在開始方塊的右邊,以下圖所示。
So let's choose the one just below, and to the right of the starting square, as is shown in the following illustration.
[圖5]
[Figure 5]
這一次,當我們檢查相鄰的方塊,我們發現,一到右邊當即是一堵牆方塊,所以我們忽略。這同樣適用於一個剛剛上面。我們還忽略略低於牆上的方塊。爲什麼呢?因爲你不能獲得該方塊直接從當前方塊沒有穿越附近的牆角切割。你真的須要往下走,而後再動過那個方塊,圍繞在這個過程中角落移動。 (注:上偷工減料這條規則是可選的它的使用依賴於你的節點如何放置。)
This time, when we check the adjacent squares we find that the one to the immediate right is a wall square, so we ignore that. The same goes for the one just above that. We also ignore the square just below the wall. Why? Because you can't get to that square directly from the current square without cutting across the corner of the nearby wall. You really need to go down first and then move over to that square, moving around the corner in the process. (Note: This rule on cutting corners is optional. Its use depends on how your nodes are placed.)
那剩下5個方塊。另外兩個方格低於當前方塊還沒有在開啓列表中,因此我們將它們添加並把當前方塊變成他們的父。其餘三個平方,有兩個已經在關閉列表(開始方塊,和一個略高於目前的方塊上,在藍色突出兩個圖中),因此我們忽略他們。而最後的方塊,眼前的當前方塊左側,進行檢查,看是否G值低了,若是你去通過當前方塊到那裏。沒有方塊了。因此,我們就大功告成了,並準備檢查開啓列表中的下一個方塊。
That leaves five other squares. The other two squares below the current square aren't already on the open list, so we add them and the current square becomes their parent. Of the other three squares, two are already on the closed list (the starting square, and the one just above the current square, both highlighted in blue in the diagram), so we ignore them. And the last square, to the immediate left of the current square, is checked to see if the G score is any lower if you go through the current square to get there. No dice. So we're done and ready to check the next square on our open list.
我們重複這個過程,直到我們添加目標方塊關閉列表,此時它看起來像下面的插圖。
We repeat this process until we add the target square to the closed list, at which point it looks something like the illustration below.
[圖6]
[Figure 6]
須要注意的是從上圖低於初始方格的兩格的父方格已經改變。以前,它的G值爲28,指向回它右上的方塊。現在它有一個值20分,並指向它上面方塊。這發生在我們的搜索,對G值被檢查那裏的方式,它居然採用了要低一些的做爲新的路徑- 這樣的父被切換,G值和F值也從新計算。在這個例子中雖然這種變化彷佛並不過重要,有不少可能的狀況下,這種持續的檢查將會使全部的差別在肯定目標的最佳路徑。
Note that the parent square for the square two squares below the starting square has changed from the previous illustration. Before it had a G score of 28 and pointed back to the square above it and to the right. Now it has a score of 20 and points to the square just above it. This happened somewhere along the way on our search, where the G score was checked and it turned out to be lower using a new path – so the parent was switched and the G and F scores were recalculated. While this change doesn't seem too important in this example, there are plenty of possible situations where this constant checking will make all the difference in determining the best path to your target.
那麼,我們如何肯定路徑?簡單,剛開始在紅色的目標方格,並努力從一個方格向後移到它的父,下面的箭頭。這最終將帶你回到開始方塊,這就是你的路徑。它應該看起來像下面的插圖。移動從開始方塊A到目標方塊B就是從路徑上每個方塊(節點)的中心移動到下一個方塊的中心的問題,直到到達目標。
So how do we determine the path? Simple, just start at the red target square, and work backwards moving from one square to its parent, following the arrows. This will eventually take you back to the starting square, and that's your path. It should look like the following illustration. Moving from the starting square A to the destination square B is simply a matter of moving from the center of each square (the node) to the center of the next square on the path, until you reach the target.
[圖7]
[Figure 7]
(待續)