找到你要的答案

Q:PathFinding algorithm within this implementation of a grid

Q:寻路算法在实现网格

Reading about Dijkstra's algorithm for pathfinding, I see that every example applicable to a "grid based" game is related to the case in which you have a "cell" that is passable or not passable. I better examplain with an image:

I need to implement an algorithm for pathfinding from A to B (returning a list of Cells to "follow") for the case II. As you can see from the image, in this model there aren't cells which are "unpassable", but every cell has stored 4 informations that determines if, while inside a cell, you can go up, down, left, right.

Searching on the net I found a lot of implementations of Dijkstra's algorithm for Case I.

  1. Is it possible to implement it for case II?
  2. If yes, can you please give me an advice?
  3. Should I use another algorithm for that case (The grid will be 32x14)?

关于寻路的Dijkstra算法的阅读,我发现每一个例子都适用于“网格化”游戏相关的案件中,你有一个“细胞”,是可以或不可以。我最好examplain与图像:

我要从A到B的寻路算法实现(返回一个列表的细胞“跟随”)为案例二。正如你可以看到图像,该模型也即“结肠”没有细胞,但每一个细胞都有存储4信息,确定,而在细胞内,你可以上,下,左,右。

我在网上的情况下,我发现了很多Dijkstra的搜索算法的实现

  1. Is it possible to implement it for case II?
  2. If yes, can you please give me an advice?
  3. Should I use another algorithm for that case (The grid will be 32x14)?
answer1: 回答1:

Yes, it is possible. Transform your cells into a graph by modelling cells as nodes, and only connect two cells with an edge, if no wall separates them.

However, Dijkstra is not the best algorithm to use, for such an easy example. If all edges in the graph have a distance of one, you can simply use a BFS search to find the quickest path.

Additionally, the fact that the path is a grid may mean that you could even find faster algorithms to solve the problem. However, this only makes sense if your grid is really big. For your 32x14 grid, I highly doubt that a sophisticated algorithm will be faster than BFS.

是的,这是可能的。通过将细胞建模为节点,将细胞转化为图形,只需连接两个边缘的细胞,如果没有墙将它们分开。

然而,Dijkstra并没有使用最好的算法,对于这样一个简单的例子。如果图中所有边的距离,你可以简单地使用一个BFS搜索找到最快的路径。

此外,路径是网格的事实可能意味着你甚至可以找到更快的算法来解决这个问题。然而,这仅仅是有道理的,如果你的网格是真的大。你的32x14网格,我非常怀疑,一个复杂的算法会比BFS。

algorithm  path-finding