关于递归函数的 StackOverflow
StackOverflow on Recursive Function
我有以下函数,它应该产生笛卡尔平面中的所有坐标,我可以在 n 步内从原点到达:
原点是'location',步数是'strength',是一个整数1-10。但是,我不断收到 Whosebug 错误。每次我调用它时,我都会在 ArrayList 位置上调用 clear。想法?
更新代码:
// Returns all positions reachable in 'strength' steps
public ArrayList<Int2D> findEscapeSpace(Int2D location, Field f) {
// Are we still within the given radius?
if((Math.abs(location.getX() - this.location.getX()) + Math.abs(location.getY() - this.location.getY())) < strength) {
System.out.println("Starting on " + location);
// If this position is not contained already, and if it doesn't contain a wall
if(!positions.contains(location) && f.wallField.getObjectsAtLocation(location) == null) {
positions.add(location);
System.out.println("added " + location);
}
// Getting neighboring positions
ArrayList<Int2D> neigh = findNeighPos(location, f);
for(Int2D pos : neigh) {
System.out.println("looking into " + pos + " at depth " + (Math.abs(location.getX() - this.location.getX()) + Math.abs(location.getY() - this.location.getY())) + " and strength " + strength);
if(!positions.contains(pos))
findEscapeSpace(pos, f);
}
}
System.out.println(positions.size());
return positions;
}
旧代码
public ArrayList<Int2D> positions = new ArrayList<Int2D>();
// Returns all positions reachable in 'strength' steps
public ArrayList<Int2D> findEscapeSpace(Int2D location, Field f) {
// Are we still within the given radius?
if((Math.abs(location.getX() - this.location.getX()) + Math.abs(location.getY() - this.location.getY())) < strength) {
// If this position is not contained already, and if it doesn't contain a wall
if(!positions.contains(location) && f.wallField.getObjectsAtLocation(location) == null)
positions.add(location);
// Getting neighboring positions
ArrayList<Int2D> neigh = findNeighPos(location, f);
for(Int2D pos : neigh) {
findEscapeSpace(pos, f);
}
}
return positions;
}
public ArrayList<Int2D> findNeighPos(Int2D currentP, Field f) {
ArrayList neighPositions = new ArrayList<Int2D>();
int cx = currentP.getX();
int cy = currentP.getY();
int maxY = f.HEIGHT-1;
int maxX = f.WIDTH-1;
// A few checks to make sure we're not going off tack (literally)
if(cx > 0 && cy < maxY)
neighPositions.add(new Int2D(cx-1, cy+1));
if(cy < maxY)
neighPositions.add(new Int2D(cx, cy+1));
if(cx < maxX && cy < maxY)
neighPositions.add(new Int2D(cx+1, cy+1));
if(cx > 0)
neighPositions.add(new Int2D(cx-1, cy));
if(cx < maxX)
neighPositions.add(new Int2D(cx+1, cy));
if(cx > 0 && cy > 0)
neighPositions.add(new Int2D(cx-1, cy-1));
if(cy > 0)
neighPositions.add(new Int2D(cx, cy-1));
if(cx < maxX && cy > 0)
neighPositions.add(new Int2D(cx+1, cy-1));
return neighPositions;
}
您的递归似乎没有终止条件。看起来您可能希望将 strength
作为参数传递给 findEscapeSpace()
,并且当该方法递归时传递的值比传递给它的值小一。
除此之外,您的算法看起来相当低效,因为它可能每次生成并测试许多可达单元格多次,而且,检查是否已找到每个单元格的成本相对较高.但这是下一个需要克服的问题。
我有以下函数,它应该产生笛卡尔平面中的所有坐标,我可以在 n 步内从原点到达:
原点是'location',步数是'strength',是一个整数1-10。但是,我不断收到 Whosebug 错误。每次我调用它时,我都会在 ArrayList 位置上调用 clear。想法?
更新代码:
// Returns all positions reachable in 'strength' steps
public ArrayList<Int2D> findEscapeSpace(Int2D location, Field f) {
// Are we still within the given radius?
if((Math.abs(location.getX() - this.location.getX()) + Math.abs(location.getY() - this.location.getY())) < strength) {
System.out.println("Starting on " + location);
// If this position is not contained already, and if it doesn't contain a wall
if(!positions.contains(location) && f.wallField.getObjectsAtLocation(location) == null) {
positions.add(location);
System.out.println("added " + location);
}
// Getting neighboring positions
ArrayList<Int2D> neigh = findNeighPos(location, f);
for(Int2D pos : neigh) {
System.out.println("looking into " + pos + " at depth " + (Math.abs(location.getX() - this.location.getX()) + Math.abs(location.getY() - this.location.getY())) + " and strength " + strength);
if(!positions.contains(pos))
findEscapeSpace(pos, f);
}
}
System.out.println(positions.size());
return positions;
}
旧代码
public ArrayList<Int2D> positions = new ArrayList<Int2D>();
// Returns all positions reachable in 'strength' steps
public ArrayList<Int2D> findEscapeSpace(Int2D location, Field f) {
// Are we still within the given radius?
if((Math.abs(location.getX() - this.location.getX()) + Math.abs(location.getY() - this.location.getY())) < strength) {
// If this position is not contained already, and if it doesn't contain a wall
if(!positions.contains(location) && f.wallField.getObjectsAtLocation(location) == null)
positions.add(location);
// Getting neighboring positions
ArrayList<Int2D> neigh = findNeighPos(location, f);
for(Int2D pos : neigh) {
findEscapeSpace(pos, f);
}
}
return positions;
}
public ArrayList<Int2D> findNeighPos(Int2D currentP, Field f) {
ArrayList neighPositions = new ArrayList<Int2D>();
int cx = currentP.getX();
int cy = currentP.getY();
int maxY = f.HEIGHT-1;
int maxX = f.WIDTH-1;
// A few checks to make sure we're not going off tack (literally)
if(cx > 0 && cy < maxY)
neighPositions.add(new Int2D(cx-1, cy+1));
if(cy < maxY)
neighPositions.add(new Int2D(cx, cy+1));
if(cx < maxX && cy < maxY)
neighPositions.add(new Int2D(cx+1, cy+1));
if(cx > 0)
neighPositions.add(new Int2D(cx-1, cy));
if(cx < maxX)
neighPositions.add(new Int2D(cx+1, cy));
if(cx > 0 && cy > 0)
neighPositions.add(new Int2D(cx-1, cy-1));
if(cy > 0)
neighPositions.add(new Int2D(cx, cy-1));
if(cx < maxX && cy > 0)
neighPositions.add(new Int2D(cx+1, cy-1));
return neighPositions;
}
您的递归似乎没有终止条件。看起来您可能希望将 strength
作为参数传递给 findEscapeSpace()
,并且当该方法递归时传递的值比传递给它的值小一。
除此之外,您的算法看起来相当低效,因为它可能每次生成并测试许多可达单元格多次,而且,检查是否已找到每个单元格的成本相对较高.但这是下一个需要克服的问题。