※※※ 外語很差湊合着看吧,呵呵 ※※※php
A *路徑搜索入門node
A* Pathfinding for Beginnersweb
帕特里克·萊斯特發表於2003年10月8日下午8點33人工智能算法
By Patrick Lester Published Oct 08 2003 08:33 PM in Artificial Intelligenceless
若是您發現本文中包含錯誤或問題,致使沒法讀取它(丟失的影像或文件,受損代碼,不正確的文本格式等),請聯繫編輯,以便能更正。感謝您幫助咱們改善此資源。ide
If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource.佈局
更新於2005年7月18日this
Updated July 18, 2005人工智能
這篇文章已被翻譯成阿爾巴尼亞語,中文,法語,德語,葡萄牙語,俄語和西班牙語。其餘的翻譯是歡迎的。在這篇文章的底部有電子郵件地址。spa
This article has been translated into Albanian, Chinese, French, German, Portuguese, Russian, and Spanish. Other translations are welcome. See email address at the bottom of this article.
對於初學者, A *(讀A-星,譯者注:老外讀A-star)算法可能有些復雜。雖然在網絡上有不少解釋A *的文章,大多數寫給有A *基礎的。這篇文章是爲真正的初學者。
The A* (pronounced A-star) algorithm can be complicated for beginners. While there are many articles on the web that explain A*, most are written for people who understand the basics already. This article is for the true beginner.
本文並不試圖成爲這一主題的權威著做。相反,它描述的基本原則並準備你去閱讀全部的其餘材料和明白他們在說什麼。在這篇文章的末尾提供鏈接到一些最好的來進一步閱讀。
This article does not try to be the definitive work on the subject. Instead it describes the fundamentals and prepares you to go out and read all of those other materials and understand what they are talking about. Links to some of the best are provided at the end of this article, under Further Reading.
最後,這篇文章不是具體方案。你應該能夠適應在這裏的任何計算機語言。正如你所指望的,然而, 在這篇文章的末尾,我有一個鏈接到本文的示例程序。例子包中包含兩個版本:一個是C++,一個是Blitz Basic(http://www.blitzbasic.com/Home/_index_.php)。它還包含可執行文件,若是你只是想看看A *的行爲。
Finally, this article is not program-specific. You should be able to adapt what's here to any computer language. As you might expect, however, I have included a link to a sample program at the end of this article. The sample package contains two versions: one in C++ and one in Blitz Basic. It also contains executables if you just want to see A* in action.
可是我們愈來愈提早。從頭開始......
But we are getting ahead of ourselves. Let's start at the beginning ...
■簡介:搜索區域
Introduction: The Search Area
假設有人想要從A點到達B點。假設有一面牆把AB兩點隔開。如面所示,用綠色表示起點A,用紅色表示結點B並用藍色填充的方塊表示中間的那面牆。
Let's assume that we have someone who wants to get from point A to point B. Let's assume that a wall separates the two points. This is illustrated below, with green being the starting point A, and red being the ending point B, and the blue filled squares being the wall in between.
[圖1]
[Figure 1]
首先,我們把搜索區域分割成正方形網格。這叫簡化搜索區域,是路徑搜索的第一步。這種方法把搜索區域簡化爲一個二維數組。數組中的每一個元素表明了網格裏的一個方塊,它的地位被記錄爲行走和不可行走。從A到B通過的方塊叫路徑。一旦路徑被找到發現,就能夠從一個正格的中心到下一個中心,直到到達目標。
The first thing you should notice is that we have divided our search area into a square grid. Simplifying the search area, as we have done here, is the first step in pathfinding. This particular method reduces our search area to a simple two dimensional array. Each item in the array represents one of the squares on the grid, and its status is recorded as walkable or unwalkable. The path is found by figuring out which squares we should take to get from A to B. Once the path is found, our person moves from the center of one square to the center of the next until the target is reached.
這些中心點被稱爲「節點」。在看別的路徑搜索資料時,經常會看到討論節點。爲什麼不叫它們方格呢?因爲有可能把路徑搜索面積不分割成方格。能夠是矩形,六邊形,三角形或任何形狀,真的。節點能夠用任何形狀表示- 在中心或者沿着邊緣,或其餘任何地方。不過,我們使用該系統,因爲它是最簡單的。
These center points are called "nodes". When you read about pathfinding elsewhere, you will often see people discussing nodes. Why not just call them squares? Because it is possible to divide up your pathfinding area into something other than squares. They could be rectangles, hexagons, triangles, or any shape, really. And the nodes could be placed anywhere within the shapes – in the center or along the edges, or anywhere else. We are using this system, however, because it is the simplest.
■開始搜索
Starting the Search
一旦把搜索區域簡化成可管理數量的節點,就像上面所說的網格佈局,下一步就是進行搜索來找到最短路徑。從A點開始,檢查相鄰的方塊並向外普及搜索,直到找到目標。
Once we have simplified our search area into a manageable number of nodes, as we have done with the grid layout above, the next step is to conduct a search to find the shortest path. We do this by starting at point A, checking the adjacent squares, and generally searching outward until we find our target.
我們執行如下操做開始搜索:
We begin the search by doing the following:
1.從起點A開始,並將它添加到 「開啓列表」。開啓列表有點像一張購物清單。儘管現在列表裏只有一個項目,但之後會有更多。它包含了可能是你想要的,也可能不是。基本上,這是須要被檢查的方塊的列表。
1.Begin at the starting point A and add it to an "open list" of squares to be considered. The open list is kind of like a shopping list. Right now there is just one item on the list, but we will have more later. It contains squares that might fall along the path you want to take, but maybe not. Basically, this is a list of squares that need to be checked out.
2.尋找起點相鄰的全部可到達或可行走的方塊,忽略有牆,水或其餘非法地形。把它們添加到開啓列表。對於每個方塊,保存A點做爲它們的「父方格」。當要追溯路徑時父方格的東西是很重要的。稍後會解釋它。
2.Look at all the reachable or walkable squares adjacent to the starting point, ignoring squares with walls, water, or other illegal terrain. Add them to the open list, too. For each of these squares, save point A as its "parent square". This parent square stuff is important when we want to trace our path. It will be explained more later.
3.從開啓列表刪除開始點A,並將它添加到不須要再次尋找的「關閉列表」。
3.Drop the starting square A from your open list, and add it to a "closed list" of squares that you don't need to look at again for now.
在這一點上,你應該有類似下面插圖的形象。在該圖中,位於中心的深綠色方塊就是開始方塊。它是輪廓爲淺藍色,以指示它已被添加到封閉列表。對在開啓列表的全部相鄰方塊進行檢查,而且用淺綠色來框記它們。每一個方格都有一個灰色的指針指回它們的父方格,也就是開始方塊。
At this point, you should have something like the following illustration. In this illustration, the dark green square in the center is your starting square. It is outlined in light blue to indicate that the square has been added to the closed list. All of the adjacent squares are now on the open list of squares to be checked, and they are outlined in light green. Each has a gray pointer that points back to its parent, which is the starting square.
[圖2]
[Figure 2]
下一步,我們選擇了開啓列表上的一個相鄰方塊,並或多或少重複前面的過程,以下所述。可是,我們選擇哪方?一個與最小F值。
Next, we choose one of the adjacent squares on the open list and more or less repeat the earlier process, as described below. But which square do we choose? The one with the lowest F cost.