Shortest path in a maze with health loss

你。 提交于 2019-12-02 02:41:49

The standard approach to problems like this is sometimes called 'graph layering'. You make 5 copies of the original graph (numbered 0 through 4 in this case), where getting to a vertext v in graph n means getting to the corresponding vertex in the original graph after suffering n deaths.

If an edge in the original graph costs you a life, then it connects a vertex in each graph i to a vertex in graph i+1, and otherwise it connects vertices in the same version of the graph just like the original.

After constructing this graph, use Dijkstra's algorithm to find the shortest path to the terminal vertex in any layer.

Just use A*, moreover decrease health every move accordingly, but consider every move that would cause health to reach 0, as if that case were an obstacle case.

Edit: for OP convenience, I will enclose a link to articles about A*: this and this and this

Explainatory Note: It might be tricky to see, A* tries greedy first, but then backtraces and will eventually find the shortest possible route. Here I am not suggesting that a case is forever labelled as an obstacle, but merely locally considered equivalent to obstacle for the current selection if it causes a death. After finding the first path, this extended A* should not stop, but use the length of this path as upper bound and backtrace, in a decisional tree, checking all open paths. It will work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!