Java 我在返回深度优先搜索时遇到问题
我正在研究一种在六边形网格中寻找路径的算法。为此,我使用深度为3的深度优先搜索。它的工作原理是找到正确的路径。唯一的问题是它没有返回它。相反,它返回一个空集Java 我在返回深度优先搜索时遇到问题,java,recursion,return,depth-first-search,Java,Recursion,Return,Depth First Search,我正在研究一种在六边形网格中寻找路径的算法。为此,我使用深度为3的深度优先搜索。它的工作原理是找到正确的路径。唯一的问题是它没有返回它。相反,它返回一个空集 public Set findPath(Game game, Point origin, Point destination, Set<Point> hasVisited, int depth) throws Exception { if (origin.equals(destination)){ Sys
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;
}
上面的一组坐标是它应该返回的。但正如您所看到的,它返回一个空集
我尝试返回findPath,而不是仅仅执行它。这样做只会产生一个分支。它不会取消不起作用的动作。我看不到代码中的问题,希望你们能帮助我。干杯 如果只有一种解决方案,请立即返回。 递归向集合中添加一个元素,递归调用并撤消添加。 这将改变设置和结果。当收集多个结果时,将复制集合
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;
}
public boolean findPath(游戏、起点、终点、集合、,
int深度)引发异常{
if(起点等于终点)){
System.out.println(“从目标需求返回:+hasVisited”);
返回true;
}
如果(深度!=0){
用于(点数为:GetEmptyNeighbor(游戏,原点)){
如果(!hasVisited.contains(emptyNeighbor)){
if(!game.ishivebrokernafterpush(origin,emptyNeighbor)){
hasvistered.add(emptyNeighbor);
game.push(origin.x,origin.y,emptyNeighbor.x,emptyNeighbor.y);
布尔值find=findPath(游戏,emptyNeighbor,
目的地、实际情况、访问量、深度-1);
如果(找到){
返回true;
}
已访问。移除(emptyNeighbor);
game.push(emptyNeighbor.x,emptyNeighbor.y,origin.x,origin.y);
}
}
}
}
System.out.println(“未找到”);
返回false;
}
只是一个提示:您可以使用&&
操作符组合if
语句并减少嵌套。A也很好。谢谢。一些建议;1) 如果在两个ifsorigin.equals(destination)
中不必要地使用了此条件,则需要执行else语句,并且应在else中有条件地执行返回。2) findPath
方法正在返回一个对象,您将其浪费在for循环中,并且无法理解为什么要添加和删除该点。这种情况下,最后一次返回总是返回空集。3) hasVisited.add和hasVisited.remove的目的是什么,因为如果您删除了,并且始终为空。如果您有循环,我建议您填充该集合,然后稍后在其上运行findPath
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;
}