简体   繁体   English

最佳实践的C ++寻路,优化

[英]C++ pathfinding with a-star, optimization

Im wondering if I can optimize my pathfinding code a bit, lets look at this map: 我想知道是否可以优化寻路代码,让我们看一下这张地图:

+ - wall, . - free, S - start, F - finish
.S.............
...............
..........+++..
..........+F+..
..........+++..
...............

The human will look at it and say its impossible, becouse finish is surrounded... But A-star MUST check all fields to ascertain, that there isnt possible road. 人类会看着它,说它不可能,因为完成被包围了……但是A明星必须检查所有领域以确定没有可能的道路。 Well, its not a problem with small maps. 好吧,小地图不是问题。 But when I have 256x265 map, it takes a lot of time to check all points. 但是当我有256x265的地图时,要花很多时间检查所有点。 I think that i can stop searching while there are closed nodes arround the finish, i mean: 我认为当结束时有封闭的节点时,我可以停止搜索,我的意思是:

+ - wall, . - free, S - start, F - finish, X - closed node
.S.............
.........XXXXX.
.........X+++X.
.........X+F+X.
.........X+++X.
.........XXXXX.

And I want to finish in this situation (There is no entrance to "room" with finish). 我想在这种情况下完成操作(完成操作“房间”没有入口)。 I thought to check h, and while none of open nodes is getting closer, then to finish... But im not sure if its ok, maybe there is any better way? 我想检查一下h,而没有一个开放的节点越来越近,然后完成...但是我不确定是否还可以,也许还有更好的方法吗?

Thanx for any replies. 感谢您的任何答复。

First of all this problem is better solved with breadth-first search , but I will assume you have a good reason to use a-star instead. 首先,通过广度优先搜索可以更好地解决此问题,但是我认为您有充分的理由改用a-star。 However I still recommend you first check the connectivity between S and F with some kind of search(Breadth-first or depth-first search). 但是,我仍然建议您首先使用某种搜索(广度优先或深度优先的搜索)检查S和F之间的连通性。 This will solve our issue. 这将解决我们的问题。

Assuming the map doesn't change, you can preprocess it by dividing it to connected components . 假设地图不变,则可以通过将其划分为已连接的组件对其进行预处理。 It can be done with a fast disjoint set data structure . 这可以通过快速的不相交集数据结构来完成。 Then before launching A* you check in constant time that the source and destination belong to the same component. 然后,在启动A *之前,请在固定时间内检查源和目标属于同一组件。 If not—no path exists, otherwie you run A* to find the path. 如果不存在,则不存在任何路径,否则您将运行A *查找路径。

The downside is that you will need additional n-bits per cell where n = ceil(log C) for C being the number of connected components. 不利的一面是,每个单元将需要其他n位,其中n = ceil(log C),因为C是连接的组件数。 If you have enough memory and can afford it then it's OK. 如果您有足够的内存并且负担得起,那就可以了。

Edit: in case you fix n being small (eg one byte) and have more than that number of components (eg more than 256 for 8-bit n) then you can assign the same number to multiple components. 编辑:如果您将n设置为较小(例如一个字节)并且具有多个组件(例如,对于8位n大于256),则可以将相同的数量分配给多个组件。 To achieve best results make sure each component-id has nearly the same number of cells assigned to it. 为了获得最佳结果,请确保为每个组件ID分配几乎相同数量的单元。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM