A* admissible heuristics on a grid with teleporters?

前端 未结 2 744
情歌与酒
情歌与酒 2021-02-01 19:33

Suppose that you have a 2D grid of cells, some of which are filled in with walls. Characters can take a step from one square to any square that is one step horizontal or vertic

2条回答
  •  暖寄归人
    2021-02-01 20:25

    Form a graph of the teleporters:

    • You have a node for each teleporter and a node for the end position.
    • You have an edge connecting each node to each other node, forming a fully connected graph.
    • For the edge weights, use the Manhattan distance between each node's destination cell (the one you go to when you enter the teleporter) and all the other nodes.

    Use Dijkstra's algorithm to calculate the shortest distance from each node to the end.

    You can now use the minimum of the distance between a particular position and all the nodes plus the pre-calculated distance from the node to the end as a heuristic function. Dijkstra's algorithm only has to be run once as a pre-processing step. However, if the number of teleporters is a large perecentage of the number of cells, you may not get any benefit over using a simpler heuristic function.

提交回复
热议问题