完成迭代深化深度优先搜索

发布于 2024-11-29 03:33:07 字数 322 浏览 5 评论 0原文

目前我有一个看起来有点像这样的物体。

C#
public class Step {
  int id;
  List<Step> nextSteps;
}

我正在尝试将其转换为另一个看起来非常相似的对象,除了它不允许循环这一事实之外。

它应该通过不扩展已经出现在更高深度的节点的子节点来处理循环。迭代深化解决了这个问题(深度优先搜索实现,但广度优先搜索顺序),但我正在努力使用以下结构的实现。

我发现的所有实现都依赖于寻找某种目标节点,而我需要扩展整个树。

任何帮助将不胜感激。 :D

At the minute I have an object that looks a bit like this.

C#
public class Step {
  int id;
  List<Step> nextSteps;
}

And I'm trying to convert it into another object that looks pretty similar apart from the fact that it doesn't allow loops.

It should handle loops by not expanding the children of nodes that have already appeared at a higher depth. Iterative deepening solves this (depth first search implementation but breadth first search order) but I'm struggling with an implementation using the following structure.

All implementations I found rely on finding some sort of goal node, whereas I need the whole tree expanded.

Any help would be appreciated. :D

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

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

发布评论

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

评论(1

心凉 2024-12-06 03:33:07

添加一个 Dictionary ,每次展开节点时,都添加它的深度。

void ExpandStep(Step s, int d)
{
    int prevDepth;
    if (lookup.TryGetValue(s, out prevDepth) && prevDepth <= d)
      return;
    lookup.Add(s, d);
    ... 
}

Add a Dictionary<Step, int> and each time you expand a node, add it with its depth.

void ExpandStep(Step s, int d)
{
    int prevDepth;
    if (lookup.TryGetValue(s, out prevDepth) && prevDepth <= d)
      return;
    lookup.Add(s, d);
    ... 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文