## 再译《A *路径搜索入门》之二 原

Path Scoring

The key to determining which squares to use when figuring out the path is the following equation:

F = G + H

where

G =从起点A沿着生成的路径到一个定的方形网格上运行成本

G = the movement cost to move from the starting point A to a given square on the grid, following the path generated to get there.

H =格子中方块到最目的地，B的运行成本通常称式，可有点混乱。因是一个猜所以这样称呼找到路径之前，真的不知道实际的距离，因各种各的事情都在途中，水等）。在本教程中给出一个H的方法，但在网计算H方法其他文章

H = the estimated movement cost to move from that given square on the grid to the final destination, point B. This is often referred to as the heuristic, which can be a bit confusing. The reason why it is called that is because it is a guess. We really don't know the actual distance until we find the path, because all sorts of things can be in the way (walls, water, etc.). You are given one way to calculate H in this tutorial, but there are many others that you can find in other articles on the web.

Our path is generated by repeatedly going through our open list and choosing the square with the lowest F score. This process will be described in more detail a bit further in the article. First let's look more closely at how we calculate the equation.

As described above, G is the movement cost to move from the starting point to the given square using the path generated to get there. In this example, we will assign a cost of 10 to each horizontal or vertical square moved, and a cost of 14 for a diagonal move. We use these numbers because the actual distance to move diagonally is the square root of 2 (don't be scared), or roughly 1.414 times the cost of moving horizontally or vertically. We use 10 and 14 for simplicity's sake. The ratio is about right, and we avoid having to calculate square roots and we avoid decimals. This isn't just because we are dumb and don't like math. Using whole numbers like these is a lot faster for the computer, too. As you will soon find out, pathfinding can be very slow if you don't use short cuts like these.

Since we are calculating the G cost along a specific path to a given square, the way to figure out the G cost of that square is to take the G cost of its parent, and then add 10 or 14 depending on whether it is diagonal or orthogonal (non-diagonal) from that parent square. The need for this method will become apparent a little further on in this example, as we get more than one square away from the starting square.

H各种方式。我里使用的方法被称曼哈方法，在计算从当前方块到目标方水平和垂直方向移动方块的总数忽略角运，忽略可能在程中的任何障碍。然后，我数乘以10，我的成本水平或垂直移一格。是（可能）被称曼哈方法，因它像算城市街区的数量从一个地方到另一个地方，在那里你不能穿过块对角。

H can be estimated in a variety of ways. The method we use here is called the Manhattan method, where you calculate the total number of squares moved horizontally and vertically to reach the target square from the current square, ignoring diagonal movement, and ignoring any obstacles that may be in the way. We then multiply the total by 10, our cost for moving one square horizontally or vertically. This is (probably) called the Manhattan method because it is like calculating the number of city blocks from one place to another, where you can't cut across the block diagonally.

Reading this description, you might guess that the heuristic is merely a rough estimate of the remaining distance between the current square and the target "as the crow flies." This isn't the case. We are actually trying to estimate the remaining distance along the path (which is usually farther). The closer our estimate is to the actual remaining distance, the faster the algorithm will be. If we overestimate this distance, however, it is not guaranteed to give us the shortest path. In such cases, we have what is called an "inadmissible heuristic."

Technically, in this example, the Manhattan method is inadmissible because it slightly overestimates the remaining distance. But we will use it anyway because it is a lot easier to understand for our purposes, and because it is only a slight overestimation. On the rare occasion when the resulting path is not the shortest possible, it will be nearly as short. Want to know more? You can find equations and additional notes on heuristics here.

FGH中的和。搜索的第一个步果可以下面的明中看出。在FGH的分数被写入在每个方格。正如在紧挨着开始方块右侧上，F被打印在左上角，G印在左下方，而H示在右下方。

F is calculated by adding G and H. The results of the first step in our search can be seen in the illustration below. The F, G, and H scores are written in each square. As is indicated in the square to the immediate right of the starting square, F is printed in the top left, G is printed in the bottom left, and H is printed in the bottom right. [Figure 3]

So let's look at some of these squares. In the square with the letters in it, G = 10. This is because it is just one square from the starting square in a horizontal direction. The squares immediately above, below, and to the left of the starting square all have the same G score of 10. The diagonal squares have G scores of 14.

H是通计到红色目的曼哈距离，水平和垂直方向，而忽略上那就是在算方式。使用种方法，方上开始的直接右3格是红格30。那么高于个方的方块的H4格距离（住，只能水平移和垂直）的一个H40.你也可以看到其他方块计H值的方法

The H scores are calculated by estimating the Manhattan distance to the red target square, moving only horizontally and vertically and ignoring the wall that is in the way. Using this method, the square to the immediate right of the start is 3 squares from the red square, for a H score of 30. The square just above this square is 4 squares away (remember, only move horizontally and vertically) for an H score of 40. You can probably see how the H scores are calculated for the other squares.

The F score for each square, again, is simply calculated by adding G and H together.

（待续）

© 著作权归作者所有 ### 如比如比

■实施上的注意事项 Notes on Implementation 现在您了解了基本的方法，当你编写自己的程序时，有一些额外的事情要考虑。下面给出我用C ++和Blitz Basic编写的程序，用其他语言也同样有效。 ...

2015/06/11
66
0
《鸡啄米C++编程入门系列》系列技术文章整理收藏

《鸡啄米C++编程入门系列》系列技术文章整理收藏 收藏整理鸡啄米C++编程入门系列文章，供个人和网友学习C++时参考 1鸡啄米：C++编程入门系列之前言 2鸡啄米：C++编程入门系列之一（进制数） ...

2015/05/26
132
0

A 路径搜索入门 帕特里克·莱斯特发表于2003年10月8日下午8点33人工智能 如果您发现文中有错误或问题（丢失的影像或文件，受损代码，不正确的文本格式等），导致无法阅读它时，请联系编辑，以...

2015/06/17
207
0
【掘金小报】第八期 怎么用 Python 实现每秒百万级的请求？

2017/05/05
0
0
《鸡啄米C++编程入门系列》系列技术文章整理收藏

《鸡啄米C++编程入门系列》已整理成PDF文档，点击可直接下载至本地查阅 https://www.webfalse.com/read/201812.html 文章 鸡啄米：C++编程入门系列之前言 鸡啄米：C++编程入门系列之一（进制...

2015/06/27
83
0

CentOS 7 搭建源码包搭建LNMP环境

1，安装epel源 yum install -y epel-release 2， 安装需要的软件 yum install -y gcc automake autoconf libtool make gcc-c++ glibc libxslt-devel \libjpeg libjpeg-devel libpng libpng......

hansonwong
40分钟前
5
0

41分钟前
2
0
ZStack的普惠云计算到底是“随大流”还是真功夫？（转自社区）

ZStack社区版
42分钟前
3
0

Shell脚本是我们写不同类型命令的一种脚本，这些命令在这一个文件中就可以执行。我们也可以逐一敲入命令手动执行。如果我们要使用shell脚本就必须在一开始把这些命令写到一个文本文件中，以后...

47分钟前
2
0
TiDB Binlog 源码阅读系列文章（四）Pump server 介绍

TiDB
51分钟前
3
0 