在我的迷宫求解器中导致我的stackoverflower的原因是什么?

时间:2015-03-17 02:36:37

标签: java recursion stack-overflow

我必须使用递归解决一个迷宫,一切都很顺利,直到我运行程序并遇到了stackoverflower错误。我在网站上读了几个其他的问题,他们都说这是因为无限递归,但是他们的问题似乎与我的完全相同。

我的netID_maze.java文件

import java.util.Random;

public class netID_Maze
{

int exitRow,
    entranceRow;
char[][] map = null;


// Method Name  : Maze (Constructor)
// Parameters  : None
// Partners  : None
// Description  : No Parameter Constructor for the maze
public netID_Maze()
{
    // omitted code generating a random maze

    setEntranceRow(rnger.nextInt(row - 2) + 1);
    setExitRow(rnger.nextInt(row - 2) + 1);

    map[getEntranceRow()][0] = '.';
    map[getExitRow()][column - 1] = '.'; 

} // end netID_Maze (without parameters)


// Method Name  : Maze (Constructor)
// Parameters  : exitTemp (int), entranceTemp(int), mapTemp (char[][])
// Partners  : None
// Description  : Parameter Constructor for the maze
public netID_Maze(char[][] mapTemp, int exitTemp, int entranceTemp)
{

    map = mapTemp;
    exitRow = exitTemp;
    entranceRow = entranceTemp;

} // end netID_Maze (with parameters)


// Method Name  : getCell
// Parameters  : row (int), column (int), character in cell (character)
// Partners  : None
// Returns : The character in the cell that's being called (character)
// Description  : Returns the character of the cell that's being called
public char getCell(int r, int c)
{
    return map[r][c];

} // end getCell()

// Method Name  : setCell
// Parameters  : row (int), column (int), character in cell (character)
// Partners  : None
// Returns : None
// Description  : Changes the character of the cell that's being called
public void setCell(int r, int c, char val)
{
    this.map[r][c] = val;

} // end setCell()

public int getEntranceRow ()
{
    return entranceRow;
}

public int getExitRow()
{
    return exitRow;
}

public void setEntranceRow(int entranceTemp)
{
    entranceRow = entranceTemp;
}

public void setExitRow(int exitTemp)
{
    exitRow = exitTemp;
}

public int getRows()
{
    return map.length;
}

public int getColumns()
{
    return map[1].length;
}

public boolean isExit(int r, int c)
{
    boolean isExit = false;

    if (getExitRow() == r && map[1].length - 1 == c)
    {
        isExit = true;
    }

    return isExit;
}

public boolean isEntrance(int r, int c)
{
    boolean isEntrance = false;

    if (getEntranceRow() == r && 0 == c)
    {
        isEntrance = true;
    }

    return isEntrance;
}

public boolean isOpen(int r, int c)
{

    boolean isOpen = true;

    if (r < 0 || c < 0 || r >= getRows() || c >= getColumns())
    {
        isOpen = false;
    }
    else if (map[r][c] == '.')
    {
        isOpen = false;
    }

    return isOpen;
}

}

和我的netID_MazeSolver.java文件

public class netID_MazeSolver {
int steps = 0;
netID_Maze maze = new netID_Maze();

public netID_MazeSolver(netID_Maze mazeTemp)
{
    setSteps(0);
    maze = mazeTemp;
}

public boolean solveMaze(int r, int c)
{
    //Finding whether Current Cell is outside the maze
    if (r < 0 || c < 0 || r >= maze.getRows() || c >= maze.getColumns()) 
    {
        return false;
    }

    //Finding whether the current cell is the exit
    if (maze.isExit(r,c) == true)
    {
        return true;
    }

    //Finding whether current cell is NOT open
    if (maze.isOpen(r,c) == false)
    {
        return false;
    }

    //Setting current cell as part of the solution path


    //Finding out whether solve maze(cell below current) == true
    if (solveMaze(r - 1,c) == true)
    {
        return true;
    }

    //Finding out whether solve maze(cell to the right of current) == true
    if (solveMaze(r,c + 1) == true)
    {
        return true;
    }

    //Finding out whether solve maze(cell to the left of current) == true
    if (solveMaze(r,c - 1) == true)
    {
        return true;
    }

    //Finding out whether solve maze(cell above current) == true
    if (solveMaze(r + 1,c) == true)
    {
        return true;
    }

    //setting current cell to NOT part of the solution path


    return false;

}

public void setSteps(int stepsTemp)
{
    steps = stepsTemp;
}

public int getSteps()
{
    return steps;
}

}

实际错误只是不断重复:         at netID_MazeSolver.solveMaze(netID_MazeSolver.java:53)         在netID_MazeSolver.solveMaze(netID_MazeSolver.java:71)

1 个答案:

答案 0 :(得分:3)

您犯的基本错误是您没有为访问过的单元格设置任何标记。因此,您的算法可以反复访问同一个单元格。如果你生成的迷宫包含任何循环,你很可能最终会在一个无限循环中导致堆栈溢出。顺便说一句,你不需要写if(maze.isOpen(r , c) == true)if(maze.isOpen(r , c))使用较少的代码提供相同的结果。

相关问题