[英]DFS shortest path of a maze in C++
I'm having trouble figuring out how exactly to get this to work... I'm attempting to get the shortest path to the goal using a DFS. 我在弄清楚如何使它正常工作方面遇到困难...我正在尝试使用DFS获得达到目标的最短路径。 I know that BFS is better but I was asked to use DFS for this. 我知道BFS更好,但是我被要求为此使用DFS。 As you can see, I attempt to make a comparison between all stacks that lead to the end to find the goal, but it does not work, only the first stack that leads to the goal is ever printed... I know somewhere I need to unvisit nodes, but I cannot figure out exactly how. 如您所见,我尝试在所有导致目标找到的堆栈之间进行比较,但是这种方法不起作用,只有第一个导致目标的堆栈才被打印出来……我知道我需要在某个地方取消访问节点,但我不知道具体如何操作。 Right now I do get a path to the goal, but not the shortest one. 现在,我确实可以达成目标,但并非最短目标。 Any help with this would be greatly appreciated. 任何帮助,将不胜感激。
Writing a non-recursive DFS is possible by using your own stack, but I find the recursive solution to be more elegant. 通过使用自己的堆栈,可以编写非递归DFS,但是我发现递归解决方案更加优雅。 Here is a sketch of one: 这是一个草图:
DFS(vertex)
path.push_back(vertex)
visited[vertex] = true
if we found the exit
output path
else
for each neighbor v of vertex
if not visited[v]
DFS(v)
visited[vertex] = false
path.pop_back()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.