我得到了一些代码,其中已经包含了构建迷宫的所有内容。我要编写 makeMove 方法来解决迷宫,我已经在这里完成了:
int MAX_ROWS = endRow + 1;
int MAX_COLS = endCol + 1;
boolean[][]visited = new boolean[MAX_ROWS][MAX_COLS];
protected void makeMove( int row, int col )
{
boolean found = false;
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 (!found) {
makeMove(row, col - 1);
makeMove(row, col + 1);
makeMove(row - 1, col);
makeMove(row + 1, col);
}
System.out.print("\n[" + row + "," + col + "] "); // display location
}//end makeMove
}
假设开始位置在左上角,结束位置在右下角,这段代码的工作方式应该是这样的。
但是,现在我需要修改它,使其只允许对角线移动......
我处于停滞状态,不知道如何实现这一目标
感谢您的帮助
最佳答案
使用
makeMove(row - 1, col - 1);
makeMove(row - 1, col + 1);
makeMove(row + 1, col - 1);
makeMove(row + 1, col + 1);
关于java - 迷宫解算器,仅对角线移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20125886/