标签:
※※※ 外语不好凑合着看吧,呵呵 ※※※
A *路径搜索入门
A* Pathfinding for Beginners
帕特里克·莱斯特发表于2003年10月8日下午8点33人工智能
By Patrick Lester Published Oct 08 2003 08:33 PM in Artificial Intelligence
如果您发现本文中包含错误或问题,导致无法读取它(丢失的影像或文件,受损代码,不正确的文本格式等),请联系编辑,以便能更正。感谢您帮助我们改善此资源。
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日
Updated July 18, 2005
这篇文章已被翻译成阿尔巴尼亚语,中文,法语,德语,葡萄牙语,俄语和西班牙语。其他的翻译是欢迎的。在这篇文章的底部有电子邮件地址。
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.
标签:
原文地址:http://my.oschina.net/u/660460/blog/425924