如何表示用于 DFS/BFS 的数据

发布于 2025-01-01 03:21:50 字数 487 浏览 0 评论 0原文

我被分配了一个问题,需要使用各种搜索技术来解决。该问题与 Escape From Zurg 问题或Bridge 和 Torch 问题。我的问题是我不知道如何将数据表示为树。

这是我对如何做到这一点的猜测,但对于搜索来说没有多大意义。

Graph

另一种方法可能是使用按行走时间排序的二叉树。然而,我仍然不确定我是否正确地解决了这个问题,因为搜索算法不一定需要二叉树。

任何有关表示此数据的提示将不胜感激。

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.

Graph

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 技术交流群。

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

发布评论

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

评论(2

身边 2025-01-08 03:21:50

通常,当您使用树搜索来解决问题时,每个节点代表世界的一些可能的“状态”(例如,谁在桥的哪一侧),每个节点的子节点代表所有可能的“后继状态” (从前一状态一次移动即可到达的新状态)。深度优先搜索表示尝试一个选项,直到它陷入死胡同,然后备份到另一个选项可用的最后状态并尝试它。广度优先搜索表示并行尝试许多选项,并查看其中第一个选项何时找到目标节点。

就编码的实际方式而言,您可以将其表示为多路树。每个节点可能包含当前状态,以及指向子节点的指针列表。

希望这有帮助!

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!

溇涏 2025-01-08 03:21:50

你可以使用这样的东西:

public class Node
{
   public int root;
   public List<Node> neighbours;
   public Node(int x)
   {
       root=x;
   }
   public void setNeighboursList(List<Node> l)
   {
       neighbours = l;
   }
   public void addNeighbour(Node n)
   {
       if(neighbours==null) neighbours = new ArrayList<Node>();
       neighbours.add(n);
   }
   ...
} 

public class Tree
{
    public Node root;
    ....
}

U could use something like this:

public class Node
{
   public int root;
   public List<Node> neighbours;
   public Node(int x)
   {
       root=x;
   }
   public void setNeighboursList(List<Node> l)
   {
       neighbours = l;
   }
   public void addNeighbour(Node n)
   {
       if(neighbours==null) neighbours = new ArrayList<Node>();
       neighbours.add(n);
   }
   ...
} 

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