A *寻路,无对角线移动

时间:2016-12-05 18:55:54

标签: c++ path-finding

我目前正在尝试使用:http://www.policyalmanac.org/games/aStarTutorial.htm

在c ++中实现A *

然而,对于第一个版本,我决定不包括对角线移动。

在c点的摘要部分中,它循环检查当前点的邻居:

for each neighbour of the current square (above, below, left, right)
    if neighbour on closed list or not walkable {
        continue
    }

    if neighbour not in open list {
        add to open list
        set parent of neighbour to current square
        update F, G, H values
    } else if neighbour is on open list {
        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.
    }

如果我只允许4个方向移动,我是否还需要检查g成本以查看该方块的路径是否更好?例如,从起点开始,起点的所有4个邻居将具有相同的g。

2 个答案:

答案 0 :(得分:0)

检查较低G成本的关键是如果最终找到更好的那个,则为该方块设置新路径。您最初可能会找到从A到C的路径。但是在稍后搜索时,您将找到到C的邻居的不同路径。如果C不在关闭列表中,则新路径的G成本为C可能低于第一个,因此您希望使用更好的G(以及F)值更新C.

答案 1 :(得分:0)

让我们分析一下情况。基本点是图中的每个边都具有相同的权重。

假设您当前处于顶点c并且您正在分析邻居n。然后,邻居n的更新距离将为distance(c) + w,其中w是统一边长。现在的问题是n是否可以通过不同的路径获得更大的距离。

假设p的前一个父n会导致更长的路径。然后,n的距离为distance(p) + w。为了使该表达式大于c的更新成本,需要保留以下内容:

distance(p) + w > distance(c) + w
distance(p) > distance(c)

所以p必须比c更远离起点。如果你使用Dijkstra的算法,这是不可能的,因为这意味着p将在c之后修复。但是你使用A *并用启发式确定顺序。因此,需要遵循以下两个条件:

distance(p) > distance(c)
distance(p) + heuristic(p) <= distance(c) + heuristic(c)
<=> heuristic(p) <= heuristic(c) - (distance(p) - distance(c))

所以它归结为你的启发式。广泛使用的曼哈顿距离启发式允许这样做。因此,如果您使用此启发式,则必须检查更低的成本。如果您的启发式方法不允许上述条件(例如Dijkstra中的常量启发式方法),则不会。