我无法返回深度优先搜索

I am having trouble with returning in depth first search

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

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 并在没有成功时取消推送。

现在发生的事情是最后的 return 似乎来自函数的 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: []
[]

上面的一组坐标应该是 return。但如您所见,return是一个空集。

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

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

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;
}