我有一个程序,它从一个文件中读取一个迷宫并将其放入一个 2D 数组中,并通过它使用递归进行导航,并将P放置在它曾经存在的任何地方,并将一个V放置在它曾经两次的地方。在我的算法中的某个地方发生了一个问题,导致迷宫只有 Vs。我已经写了很多println
语句来试图调试这个问题。我是 Java 新手,我可能遗漏了一些明显的东西,但我一辈子都找不到问题所在。
迷宫代码:
public static boolean goNorth(){
boolean success;
if(maze[currCol][currRow - 1] == CLEAR){
currRow = currRow - 1;
maze[currCol][currRow] = PATH;
if (maze[currCol][currRow] == maze[finishCol][finishRow]){
success = true;
} else {
success = goNorth();
if(!success){
success = goWest();
if(!success){
success = goEast();
if(!success){
maze[currCol][currRow] = VISITED;
currRow = currRow + 1;
}
}
}
}
} else {
success = false;
}
return success;
}
public static boolean goWest(){
boolean success;
if(maze[currCol - 1][currRow] == CLEAR){
currCol = currCol - 1;
maze[currCol][currRow] = PATH;
if (maze[currCol][currRow] == FINISH){
success = true;
} else {
success = goWest();
if(!success){
success = goSouth();
if(!success){
success = goNorth();
if(!success){
maze[currCol][currRow] = VISITED;
currCol = currCol + 1;
}
}
}
}
} else {
success = false;
}
return success;
}
public static boolean goEast(){
boolean success;
if(maze[currCol + 1][currRow] == CLEAR){
currCol = currCol + 1;
maze[currCol][currRow] = PATH;
if (maze[currCol][currRow] == FINISH){
success = true;
} else {
success = goEast();
if(!success){
success = goNorth();
if(!success){
success = goSouth();
if(!success){
maze[currCol][currRow] = VISITED;
currCol = currCol - 1;
}
}
}
}
} else {
success = false;
}
return success;
}
public static boolean goSouth(){
boolean success;
if(maze[currCol][currRow + 1] == CLEAR){
currRow = currRow + 1;
maze[currCol][currRow] = PATH;
if (maze[currCol][currRow + 1] == FINISH){
success = true;
} else {
success = goSouth();
if(!success){
success = goEast();
if(!success){
success = goWest();
if(!success){
maze[currCol][currRow] = VISITED;
currRow = currRow - 1;
}
}
}
}
} else {
success = false;
}
return success;
}
期望的输出:
xxxxxxxxxxxxxxxxxxFx
xVVVVVxPPPPPPPxxxxPx
xVxxxxxPxxxxxPPPxxPx
xVxxxxxPxxxxxxxPxxPx
xVVVVVVPPPPPPxxPxxPx
xVxxxxxxxxxxPxxPPPPx
xxxxxxxxxxxxSxxxxxxx
我得到的输出:
xxxxxxxxxxxxxxxxxxVx
xVVVVVxVVVVVVVxxxxVx
xVxxxxxVxxxxxVVVxxVx
xVxxxxxVxxxxxxxVxxVx
xVVVVVVVVVVVVxxVxxVx
xVxxxxxxxxxxVxxVVVVx
xxxxxxxxxxxxSxxxxxxx