【问题标题】:I am having trouble with returning in depth first search我无法返回深度优先搜索
【发布时间】:2020-03-15 12:40:08
【问题描述】:

我正在研究一种在六边形网格中查找路径的算法。为此,我使用深度为 3 的深度优先搜索。它的工作原理是找到正确的路径。唯一的问题是它不返回它。相反,它返回一个空集。

public Set findPath(Game game, Point origin, Point destination, Set<Point> hasVisited, int depth) throws Exception {
    if (origin.equals(destination)){
        System.out.println("Return from goal requirements: " + hasVisited);
        return hasVisited;
    }

    if (!origin.equals(destination)){
        if (depth != 0) {
            for (Point emptyNeighbor : getEmptyNeighbors(game, origin)) {
                if (!hasVisited.contains(emptyNeighbor)) {
                    if (!game.isHiveBrokenAfterPush(origin, emptyNeighbor)) {
                        hasVisited.add(emptyNeighbor);
                        game.push(origin.x, origin.y, emptyNeighbor.x, emptyNeighbor.y);

                        findPath(game, emptyNeighbor, destination, actualOrigin, hasVisited, depth - 1);

                        hasVisited.remove(emptyNeighbor);
                        game.push(emptyNeighbor.x, emptyNeighbor.y, origin.x, origin.y);
                    }
                }
            }
        }
    }

    System.out.println("Return from end of function: " + hasVisited);
    return hasVisited;
}

在加载完 If 语句后,它会将节点添加到 hasVisited 并朝该方向推送片段。然后它调用自己继续在树的分支上。它会移除节点表单 hasVisited,如果不成功则取消推送。

现在发生的事情是最终返回似乎来自函数的 and。这些是它打印的最后几行:

Return from goal requirements: [java.awt.Point[x=-1,y=0], java.awt.Point[x=-2,y=1], java.awt.Point[x=-2,y=2]]
Return from end of function: [java.awt.Point[x=-1,y=0], java.awt.Point[x=-2,y=1]]
Return from end of function: [java.awt.Point[x=-1,y=0]]
Return from end of function: []
[]

上面的坐标集是它应该返回的。但如您所见,它返回一个空集。

我试图返回 findPath 而不是仅仅执行它。这样做只做一个分支。它不会取消不起作用的动作。我在我的代码中看不到问题,希望你们能帮助我。干杯!

【问题讨论】:

  • 提示:您可以使用&amp;&amp; 运算符组合if 语句并减少嵌套。 minimal reproducible example 也不错。谢谢。
  • 几点建议; 1)你在两个ifsorigin.equals(destination)中不必要地使用了这个条件,你需要做一个else语句并且return应该在else中有条件地完成。 2)findPath 方法正在返回一个对象,您将其浪费在 for 循环中,并且为什么添加和删除该点没有意义。这就是最后一次返回总是返回空集的情况。 3) hasVisited.add 和 hasVisited.remove 的目的是什么,因为如果你删除并且你总是空的。如果您有循环,我建议您填充集合,然后稍后在其上运行 findPath

标签: java recursion return depth-first-search


【解决方案1】:

如果只有一种解决方案,请立即返回。 递归将一个元素添加到集合中,递归调用并撤消添加。 这将改变集合,结果。当收集多个结果时,将复制该集合。

public boolean findPath(Game game, Point origin, Point destination, Set<Point> hasVisited,
            int depth) throws Exception {
    if (origin.equals(destination)){
        System.out.println("Return from goal requirements: " + hasVisited);
        return true;
    }

    if (depth != 0) {
        for (Point emptyNeighbor : getEmptyNeighbors(game, origin)) {
            if (!hasVisited.contains(emptyNeighbor)) {
                if (!game.isHiveBrokenAfterPush(origin, emptyNeighbor)) {
                    hasVisited.add(emptyNeighbor);
                    game.push(origin.x, origin.y, emptyNeighbor.x, emptyNeighbor.y);
                    boolean found = findPath(game, emptyNeighbor,
                            destination, actualOrigin, hasVisited, depth - 1);
                    if (found) {
                        return true;
                    }

                    hasVisited.remove(emptyNeighbor);
                    game.push(emptyNeighbor.x, emptyNeighbor.y, origin.x, origin.y);
                }
            }
        }
    }

    System.out.println("Not found");
    return false;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    相关资源
    最近更新 更多