[英]Recursive Maze Solver Issue
I'm making a recursive Java maze program and I'm stuck on the part when it comes to calling to my subroutines goNorth()
, goWest()
, goEast()
and goSouth()
. 我正在
goNorth()
一个递归Java迷宫程序,在调用子例程goNorth()
, goWest()
, goEast()
和goSouth()
。 Basically my problem involves the fact that it calls to one subroutine, but within that subroutine it doesn't pick up on my other else if and else statements therefore not making it pick up on the other possibilities. 基本上,我的问题涉及以下事实:它调用了一个子例程,但是在该子例程中,if和else语句没有对我的其他子例程起作用,因此不会使它在其他可能性上起作用。 Please assist, I appreciate your upcoming answer.
请协助,感谢您即将收到的答复。
import java.util.*;
import java.io.*;
public class RecursiveMazeSolverv2 {
static Scanner in;
static int row = 0;
static int col = 0;
static char maze[][];
static int rowSize = 0;
static int colSize = 0;
static boolean success = true;
public static void main(String[] args) {
while (true) {
try {
in = new Scanner(new File("Maze1.txt"));
break;
}
catch (FileNotFoundException e) {
System.out.println("Wrong File");
}
}
rowSize = Integer.parseInt(in.nextLine());
colSize = Integer.parseInt(in.nextLine());
maze = new char[rowSize][colSize];
String lineOfInput = "";
for (int i = 0; i < maze.length; i++) {
lineOfInput = in.nextLine();
for (int j = 0; j < maze.length; j++) {
maze[i][j] = lineOfInput.charAt(j);
}
}
displayGrid();
for (int i = 0; i < maze.length; i++) {
for (int j = 0; j < maze.length; j++) {
if (maze[i][j] == 'S') {
maze[i][j]='+';
System.out.println("Starting coordinates: " + i + ", " + j);
row = i;
col = j;
}
}
}
if (goNorth(row, col))
displayGrid();
else
System.out.println("Impossible!");
}
static Boolean goNorth(int row, int col) {
if (maze[row][col] == '.') {
maze[row][col] = '+';
return goNorth(row -1, col);
}
else if (maze[row][col] == 'G') {
return true;
}
else {
success = goNorth(row, col);
if (success == false) {
success = goWest(row, col -1);
}
if (success == false) {
success = goEast(row, col +1);
}
if (success == false) {
success = goSouth(row +1, col);
}
if (success == false) {
maze[row][col] = '.';
success = false; }
return false;
}
}
static Boolean goWest(int row, int col) {
if (maze[row][col] == '.') {
maze[row][col] = '+';
return goWest(row, col -1);
}
else if (maze[row][col] == 'G') {
return true;
}
else {
success = goWest(row, col);
if (success == false) {
success = goNorth(row -1, col);
}
if (success == false) {
success = goSouth(row +1, col);
}
if (success == false) {
success = goEast(row, col -1);
}
if (success == false) {
maze[row][col] = '.';
success = false; }
return false;
}
}
static Boolean goEast(int row, int col) {
if (maze[row][col] == '.') {
maze[row][col] = '+';
return goEast(row, col +1);
}
else if (maze[row][col] == 'G') {
return true;
}
else {
success = goEast(row, col);
if (success == false) {
success = goNorth(row -1, col);
}
if (success == false) {
success = goSouth(row +1, col);
}
if (success == false) {
success = goWest(row, col -1);
}
if (success == false) {
maze[row][col] = '.';
success = false; }
return false;
}
}
static Boolean goSouth(int row, int col) {
if (maze[row][col] == '.') {
maze[row][col] = '+';
return goSouth(row +1, col);
}
else if (maze[row][col] == 'G') {
return true;
}
else {
success = goSouth(row, col);
if (success == false) {
success = goNorth(row -1, col);
}
if (success == false) {
success = goWest(row, col -1);
}
if (success == false) {
success = goEast(row, col +1);
}
if (success == false) {
maze[row][col] = '.';
success = false; }
return false;
}
}
public static void displayGrid() {
for (int j = 0; j < maze.length; j++) {
for (int k = 0; k < maze.length; k++) {
System.out.print(maze[j][k] + " ");
}
System.out.println();
}
}
}
Sorry, I can't post the actual maze in here, it wont display right. 抱歉,我无法在此处发布实际的迷宫,它不会正确显示。
Problems I see: 我看到的问题:
static
. static
。 Without going into a super long and boring discussion, making everything static
means that the values you set in your recursive calls will modify the value for all calls. static
意味着您在递归调用中设置的值将修改所有调用的值。 You are effectively over-writing each recursive call with values you are setting in subsequent recursive calls. Edit - Example 编辑-示例
I will try to do this in a way to guide you without giving you a copy/paste answer, so this will be pseudo-code'ish. 我将尝试以某种方式指导您而不给您复制/粘贴答案,因此这将是伪代码。
/**
* My move recursion
*/
public boolean move(int currRow, int currCol) {
// See if we solved it...
if (solved) {
return true;
}
// Try to go north first...
if (maze[currRow-1][currCol] == '.') {
if (move(int currRow-1, currCol)) {
// Mark this with the "good" path and return true
}
}
// Try to go east next...
if (maze[currRow][currCol+1] == '.') {
if (move(int currRow, currCol+1)) {
// Mark this with the "good" path and return true
}
}
// Try to go south next...
if (maze[currRow+1][currCol] == '.') {
if (move(int currRow+1, currCol)) {
// Mark this with the "good" path and return true
}
}
// Try to go west...
if (maze[currRow][currCol-1] == '.') {
if (move(int currRow, currCol-1)) {
// Mark this with the "good" path and return true
}
}
return false;
}
So, basically we check if we are "solved". 因此,基本上,我们检查是否“已解决”。 If not, see if we can go north.
如果没有,看看我们是否可以向北走。 If we can, see if the next call is solved.
如果可以,请查看下一个呼叫是否已解决。 Repeat for east, south, and west.
重复东部,南部和西部。 Eventually one of the recursive calls will get to the solved condition, which will trigger each of the recursive calls to pass the inner if, which marks the maze and returns true, thus creating a chain reaction that ends up popping back up the call stack until you are done with the recursion.
最终,其中一个递归调用将达到已解决的条件,这将触发每个递归调用传递内部if,这会标记迷宫并返回true,从而产生连锁反应,最终连锁反应弹出调用堆栈,直到您完成了递归操作。
Things to note with recursion: 递归注意事项:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.