AStar Search - 如何更改方向和费用?

AStar Search - How to change directions and costs?

因此,在我正在研究的 A* 搜索实现中,我可以使用向上、向左、向右、向下这样的寻路方向:

#define the moving direction: up /left /right /down
xs = (0, -1, 1, 0)
ys = (-1, 0, 0, 1)

如何将移动方向更改为:上、左、右、下、左上、左下、右上、右下?

至于成本,上,左,右,下我有1.0,但如果我必须将成本作为2.0 方向:左上,左下,右上和右下,怎么能我实现了吗?

def get_cost(self, x1, y1, x2, y2):
    if x1 == x2 or y1 == y2:
      return 1.0

好吧,你可以简单地检查一下,看看你是否在每个轴上都处于相同的位置。

def get_cost(self, x1, y1, x2, y2):
    if (x1 == x2 and y1 != y2) or (x1 != x2 and y1 == y2):
        # you have just moved in one axis
        return 1.0
    else:
        return 2.0