BFS在网格中找到所有最短路径?
我正在尝试实施BFS,以在网格中找到所有最短的路径。请注意,这不是一个,不是两个,而是全部。 让我们以9个节点的图表。连接是无方向性的,节点放置在网格状格式的节点中,如下:
0 1 2
3 4 5
6 7 8
每个数字表示一个节点的位置,并且所有节点之间都有链接,上面,下方,左下方或彼此右侧的所有节点之间都有链接。 这意味着0连接到1和3,而4连接到1、3、5和7等。
我试图从0到8的所有最短路径,这意味着总共6条路径。当我实施传统的BFS算法时,我注意到我缺少一些途径,因为代码将节点放入一组可见节点的方式。
例如,从0开始访问1和3之后,代码转移到0-1-2和0-1-4,之后仅查看0-3-6,而不是0-3-4,因为4通过节点1解析。
是否有办法通过BFS解决此问题?还是我应该使用其他搜索算法?先感谢您。
I am trying to implement BFS to find ALL shortest paths in a grid. Note that this means not one, not two, but all.
Let's take a graph of 9 nodes for example. The connections are undirected, and the nodes are placed in nodes of a grid-like format, like below:
0 1 2
3 4 5
6 7 8
Each number denotes a node's placement, and there are links between all nodes either directly above, below, to the left, or to the right of each other.
This means that 0 is connected to 1 and 3, while 4 is connected to 1, 3, 5, and 7, and so on.
I am trying to get ALL the shortest paths from 0 to 8, which would mean 6 paths in total. When I implemented the traditional BFS algorithm, I noticed that I was missing a few paths because of the manner in which the code places nodes into the set of seen nodes.
For example, after visiting 1 and 3 from 0, the code moved on to 0 - 1 - 2 and 0 - 1 - 4, after which it only looked at 0 - 3 - 6 and not 0 - 3 - 4 because 4 had already been parsed through with node 1.
Is there a way to get around this through BFS? Or should I be using a different searching algorithm? Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论