在尝试运行此代码时,我遇到了错误,但在编译时却没有。在每个定向方法结束时,我按照应该进入的顺序调用下一个方法。我要做的是制作一个程序,该程序将从文件中读取迷宫并解决它。在解决它时,我希望程序为它所走的路径放置一个 P,为已经访问过的地方放置一个 V。程序只允许向左向右然后向每个方向返回。当你走向不同的方向时,你仍然必须遵守这些规则,除了你现在面对的方式是新的前锋。
迷宫代码:
public static boolean goNorth(){
boolean success;
if(maze[startCol][startRow] == maze[finishCol][finishRow]){
return true;
}
if(maze[startCol][startRow - 1] == CLEAR){
maze[startCol][startRow - 1] = PATH;
startRow = startRow - 1;
goNorth();
if(maze[startCol][startRow - 1] == PATH){
maze[startCol][startRow - 1] = VISITED;
startRow = startRow - 1;
goNorth();
if (maze[startCol][startRow - 1] == VISITED){
goSouth();
}
}
}
goWest();
return true;
}
public static boolean goEast(){
boolean success;
if(maze[startCol][startRow] == maze[finishCol][finishRow]){
return true;
}
if(maze[startCol + 1][startRow] == CLEAR){
maze[startCol + 1][startRow] = PATH;
startCol = startCol + 1;
goEast();
if(maze[startCol + 1][startRow] == PATH){
maze[startCol + 1][startRow] = VISITED;
startCol = startCol + 1;
goEast();
if(maze[startCol + 1][startRow] == VISITED){
goWest();
}
}
}
goNorth();
return false;
}
public static boolean goSouth(){
boolean success;
if(maze[startCol][startRow] == maze[finishCol][finishRow]){
return true;
}
if(maze[startCol][startRow + 1] == CLEAR){
maze[startCol][startRow + 1] = PATH;
startRow = startRow + 1;
goSouth();
if(maze[startCol][startRow + 1] == PATH){
maze[startCol][startRow + 1] = VISITED;
startRow = startRow + 1;
goSouth();
if (maze[startCol][startRow + 1] == VISITED){
goNorth();
}
}
}
goEast();
return false;
}
public static boolean goWest(){
boolean success;
if(maze[startCol][startRow] == maze[finishCol][finishRow]){
return true;
}
if(maze[startCol - 1][startRow] == CLEAR){
maze[startCol - 1][startRow] = PATH;
startCol = startCol - 1;
goWest();
if(maze[startCol - 1][startRow] == PATH){
maze[startCol - 1][startRow] = VISITED;
startCol = startCol - 1;
goWest();
if(maze[startCol - 1][startRow] == VISITED){
goEast();
}
}
}
goSouth();
return false;
}
}
迷宫.txt:
20 7
0 18
6 12
xxxxxxxxxxxxxxxxxx x
x x xxxx x
x xxxxx xxxxx xx x
x xxxxx xxxxxxx xx x
x xx xx x
x xxxxxxxxxx xx x
xxxxxxxxxxxx xxxxxxx
错误: