Java 在游戏配置单元中搜索蜘蛛实现的一个分支后,递归停止
我们正在执行游戏规则,称为。在游戏中,蜘蛛片只能移动三个六边形 我们正在尝试使用dfs查找路径,但问题是我们的dfs搜索在查看一个分支后停止 我们试图利用深度-1。因此,当深度=0时,它应该转到下一个分支。但返回语句似乎有问题 这个递归有什么问题Java 在游戏配置单元中搜索蜘蛛实现的一个分支后,递归停止,java,recursion,depth-first-search,Java,Recursion,Depth First Search,我们正在执行游戏规则,称为。在游戏中,蜘蛛片只能移动三个六边形 我们正在尝试使用dfs查找路径,但问题是我们的dfs搜索在查看一个分支后停止 我们试图利用深度-1。因此,当深度=0时,它应该转到下一个分支。但返回语句似乎有问题 这个递归有什么问题 public Set findPathForSpider(Game game, Point origin, Point destination, Point actualOrigin, Set<Point> hasVisited, int
public Set findPathForSpider(Game game, Point origin, Point destination, Point actualOrigin, Set<Point> hasVisited, int depth) throws Exception {
if(origin.equals(destination)) {
game.moveWithoutRestriction(destination.x, destination.y, actualOrigin.x, actualOrigin.y);
return hasVisited;
}
if(depth == 0) {
return new HashSet();
}
hasVisited.add(origin);
for (Point emptyNeighbor : getEmptyNeighbors(game, origin)) {
if (!hasVisited.contains(emptyNeighbor)) {
if (!game.isHiveBrokenAfterPush(origin, emptyNeighbor)) {
hasVisited.add(emptyNeighbor);
game.moveWithoutRestriction(origin.x, origin.y, emptyNeighbor.x, emptyNeighbor.y);
return findPathForSpider(game, emptyNeighbor, destination, actualOrigin, hasVisited, depth - 1);
}
}
}
return new HashSet();
}
public Set findPathForSpider(游戏游戏、点原点、点目的地、点实际值、集已访问、整数深度)抛出异常{
if(起点等于终点)){
无限制移动(destination.x,destination.y,actualOrigin.x,actualOrigin.y);
回访;
}
如果(深度==0){
返回新的HashSet();
}
hasviested.add(来源);
用于(点数为:GetEmptyNeighbor(游戏,原点)){
如果(!hasVisited.contains(emptyNeighbor)){
if(!game.ishivebrokernafterpush(origin,emptyNeighbor)){
hasvistered.add(emptyNeighbor);
无限制移动游戏(origin.x,origin.y,emptyNeighbor.x,emptyNeighbor.y);
return findPathForSpider(游戏、清空尼格尔、目的地、实际洛里金、已访问、深度-1);
}
}
}
返回新的HashSet();
}
我们在游戏类中的spiderRestrictions中调用findPathForSpider。递归深度为3
public boolean spiderRestrictions(Point originPoint, Point destinationPoint) throws Exception{
Set path = b.findPathForSpider(this, originPoint, destinationPoint, originPoint, new HashSet<>(), 3);
return !path.isEmpty() &&
!originPoint.equals(destinationPoint) &&
b.isHexagonEmpty(destinationPoint);
}
public boolean spiderRestrictions(点originPoint,点destinationPoint)引发异常{
Set path=b.findPathForSpider(this,originPoint,destinationPoint,originPoint,new HashSet(),3);
return!path.isEmpty()&&
!originPoint.equals(destinationPoint)&&
b、 ISHEXAGONEPTY(目的地);
}
请分享如何从调用方法(而不是从自身)找到PathForSpider@JeremyKahan您是指我们如何调用该方法吗?是的。我想知道一开始的深度是多少,它是3。我刚刚更新了帖子。谢谢。这不是它,只是它清楚地表明,当深度=0时,你不会进入下一个分支。在if(depth==0){returnnewhashset();}中,我认为应该对getSpider进行某种调用。但我不确定,对不起。