穿过迷宫的最短路径

发布于 2024-09-11 08:38:55 字数 207 浏览 1 评论 0原文

我正在开发一个项目,我必须使用左手法则遍历迷宫,并根据程序遇到的交叉点,我需要创建一个节点来连接到一个图,然后我将确定最短路径。目标是让程序运行穿过迷宫,然后关闭程序并从包含图形的文件中读取并确定到达终点的最短路径。我所做的是我可以使用左手法则穿过迷宫。我想做的是,当我找到交叉点时创建一个节点,每次程序移动时,我都会将该路径的成本增加一。顺便说一句,使用 dijkstra 算法时是否需要邻接矩阵?

I am working on a project that i must traverse a maze using the left-hand rule and based upon the intersections the program comes upon i need to create a node to connect to a graph that I will then determine the shortest path. The goal is for the program to run through the maze then close out the program and read from a file that contains the graph and determines the shortest path to the finish. what i have done is i can traverse the maze using the left-hand rule. what im thinking to do is create a node when i find the intersection and there after every time the program moves i increase the cost of that path by one. on a side note do you need to have an adjacency matrix when use dijkstra's algorithm?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔落惊风雨 2024-09-18 08:38:55

尝试这样的操作,它应该可以工作:

0 - create an empty "solution path" stack of location objects.

1 - if current position is maze exit, return "solution path" stack.

2 - wall in front? turn left and repeat 2, else continue to 3.

3 - if current position is at top of "solution path" stack, 
       pop it off of the stack
       else push it onto the stack 

4 - move forward.

当您检查堆栈顶部的当前位置时,您可能需要检查最后一个元素之前的元素,因为最后一个元素将是您刚刚离开的位置。

Try something like this, it should work:

0 - create an empty "solution path" stack of location objects.

1 - if current position is maze exit, return "solution path" stack.

2 - wall in front? turn left and repeat 2, else continue to 3.

3 - if current position is at top of "solution path" stack, 
       pop it off of the stack
       else push it onto the stack 

4 - move forward.

When you're checking the top of the stack for the current position, you might need to check the element just before the very last one, since the last one will be the position you just left.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文