如何表示用于 DFS/BFS 的数据
我被分配了一个问题,需要使用各种搜索技术来解决。该问题与 Escape From Zurg 问题或Bridge 和 Torch 问题。我的问题是我不知道如何将数据表示为树。
这是我对如何做到这一点的猜测,但对于搜索来说没有多大意义。
另一种方法可能是使用按行走时间排序的二叉树。然而,我仍然不确定我是否正确地解决了这个问题,因为搜索算法不一定需要二叉树。
任何有关表示此数据的提示将不胜感激。
I was assigned a problem to solve using various search techniques. The problem is very similar to the Escape From Zurg problem or the Bridge and Torch problem. My issue is that I am lost as to how to represent the data as a tree.
This is my guess as to how to do it, but it doesn't make much sense for searching.
Another way could be to use a binary tree sorted by their walking time. However, I'm still not sure if I'm attacking this problem correctly since search algorithms don't necessarily require binary trees.
Any tips on representing this data would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,当您使用树搜索来解决问题时,每个节点代表世界的一些可能的“状态”(例如,谁在桥的哪一侧),每个节点的子节点代表所有可能的“后继状态” (从前一状态一次移动即可到达的新状态)。深度优先搜索表示尝试一个选项,直到它陷入死胡同,然后备份到另一个选项可用的最后状态并尝试它。广度优先搜索表示并行尝试许多选项,并查看其中第一个选项何时找到目标节点。
就编码的实际方式而言,您可以将其表示为多路树。每个节点可能包含当前状态,以及指向子节点的指针列表。
希望这有帮助!
Generally when you are using a tree search to solve a problem, each node represents some possible "state" of the world (who's on what side of the bridge, for example), and the children of each node represent all possible "successor states" (new states that can be reached in one move from the previous state). A depth-first search then represents trying one option until it dead-ends, then backing up to the last state where another option was available and trying it out. A breadth-first search represents trying out lots of options in parallel and seeing when the first of them find the goal node.
In terms of the actual way of encoding this, you would represent this as a multiway tree. Each node would probably contain the current state, plus a list of pointers to child nodes.
Hope this helps!
你可以使用这样的东西:
U could use something like this: