我得到了包含建立一个迷宫所需的所有代码。我的工作是编写用于解决迷宫问题的makeMove
方法。 这是我到目前为止有:行和列迷宫递归误差
protected void makeMove(int row, int col)
{
int MAX_ROWS = maze.length;
int MAX_COLS = maze.length;
boolean found = false;
boolean[][]visited = new boolean[MAX_ROWS][MAX_COLS];
//visited[startRow][startCol] = true;
if (row < 0 || row >= MAX_ROWS || col < 0 || col >= MAX_COLS || visited[row][col] || maze[row][col] == 1)
return;
visited[row][col] = true;
found = row == endRow && col == endCol;
/*if(row == endRow && col == endCol) {
found = true;
}*/
if(!found && maze[row][col - 1]!=1 && !visited[row][col]) { // move left
makeMove(row, col -1);
visited[row][col -1] = true;
}
if(!found && maze[row - 1][col]!=1 && !visited[row-1][col]) { // move up
makeMove(row-1, col);
visited[row-1][col] = true;
}
if(!found && maze[row][col + 1]!=1 && !visited[row][col + 1]) { // move right
makeMove(row, col + 1);
visited[row][col + 1] = true;
}
if(!found && maze[row + 1][col]!=1 && !visited[row + 1][col]) { // move down
makeMove(row + 1, col);
visited[row + 1][col] = true;
}
这样当有8行8列运行在一个迷宫,我不断收到一个堆栈溢出错误。
相信错误表明这是在第42行和第50行,这将是
42. MakeMove(row-1, col); //to move up
和50.makeMove(row + 1, col); //to move down
。
有我在这两个做了一个逻辑上的错误?