简体   繁体   中英

StackOverflow error due to recursion in Java

I'm attempting to use recursive methods to solve a maze represented by an int array of 1s and 0s in Java. wasHere[] checks if the method has already parsed that particular set of coordinates, correctPath[] records the answer path to the maze; both of them are initialized with every index being false.

import java.awt.Point;
class mazeSolver{
   int[][] maze;
   int startX, startY; 
   int endX, endY;     
   int width, height;
   boolean[][] wasHere, correctPath;

   mazeSolver(int[][] m, int sX,int sY,int nX,int nY){
      maze = m;
      width = maze[0].length;
      height = maze.length;
      startX = sX;
      startY = sY;
      endX = nX;
      endY = nY;
      System.out.println("Height: " + height + "\t Width: " + width);

      correctPath = new boolean[height][width];
      wasHere = new boolean[height][width];
      for (int row = 0; row < maze.length; row++)  
         for (int col = 0; col < maze[row].length; col++){
            wasHere[row][col] = false;
            correctPath[row][col] = false;
         }
      solveMaze(startX,startY);
   }

     public boolean solveMaze(int x, int y){
      boolean solvable = recursiveSolve(x,y);
      return solvable;
     }

     private boolean recursiveSolve(int x, int y){ //1s are walls, 0s are free space
      if (x == endX && y == endY)
         return true;
      if (wasHere[y][x] || maze[y][x] == 1)
         return false;

      wasHere[y][x] = true;

      if (y < height-1){
         if (solveMaze(x,y+1))
            return true;
      }

      if (y > 0){
         if (solveMaze(x,y-1))
            return true;
      }

      if (x > 0){
         if (solveMaze(x-1,y))
            return true;
      }

      if (x < width-1){
         if (solveMaze(x+1,y)) 
            return true;
      }

      return false;
   }

   public int[][] getSolvedMaze(){
      for(int y = 0; y < height; y++)
         for (int x = 0; x< width; x++)
            if(correctPath[y][x])
               maze[y][x] = 2;
      return maze;
   }
}

I have been stuck with this error for the past few hours, any help would be greatly appreciated.

I think there is no bug as such (not that I can see) but you are recursing way too much which caused this issue. I am not sure what values are you feeding into the program, but I was able to notice the issue with initializing the above program with these parameters:

mazeSolver MazeSolver = new  mazeSolver(new int [100][100], 0, 0, 100,100);

So to run your program as-it-is I increased the stack size. You could do so by feeding the following parameter to JVM as you run the program: -Xss258m

This stack size was able to accommodate array of 1000 size. I did not have much time to test other sizes.

mazeSolver MazeSolver = new  mazeSolver(new int [1000][1000], 0, 0, 1000,1000); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM