我使用的迷宫算法在某些时候有效,但并非一直有效。它使用递归,但我无法弄清楚为什么它一直没有。
public boolean findPath(int x, int y) {
myArray[x][y] = "T"; // marks coordinate T for traveled
printMaze(); // prints the current state of the maze
if (x == finish[0] && y == finish[1]) { // base case: if at finish, solved.
isFinish = true;
return isFinish;
} else {
isFinish = false; // not at finish
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i * j == 0 && i != j && canIGoHere(x+i, y+j) && !isFinish) {
isFinish = findPath(x + i, y + j);
}
}
}
return isFinish;
}
}