gpt4 book ai didi

java - 是什么导致我的迷宫求解器出现 stackoverflowerror?

转载 作者:行者123 更新时间:2023-11-29 05:09:40 24 4
gpt4 key购买 nike

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

我的 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;
}

}

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

最佳答案

您犯的基本错误是您没有为访问的单元格设置任何标志。因此,您的算法可以一次又一次地访问同一个单元格。如果您生成的迷宫包含任何循环,您很可能会陷入无限循环,从而导致计算器溢出。顺便说一句,您不需要编写 if(maze.isOpen(r , c) == true)if(maze.isOpen(r , c)) 用更少的代码给出相同的结果。

关于java - 是什么导致我的迷宫求解器出现 stackoverflowerror?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29090395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com