我正在製作一個遞歸Java迷宮程序,當涉及到調用我的子程序goNorth()
,goWest()
,goEast()
和goSouth()
時,我被卡在部件上。基本上我的問題涉及這樣一個事實,即它調用一個子例程,但在該子例程中,它不會接受我的其他else和else語句,因此不會使它接受其他可能性。請協助,我感謝您即將到來的答案。遞歸迷宮求解器問題
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();
}
}
}
對不起,我不能在這裏發佈實際的迷宮,它不會顯示正確。
「但在該子程序中,它不會在我的其他人身上找到如果和其他語句,因此不會讓它接受其他可能性。」請改述:我無法理解你的意思。 – vandale
瞭解對象如何工作以及「靜態」關鍵字首先執行的操作。一切都不應該是靜態的。這是一個體面的對象教程。 http://www.tutorialspoint.com/java/java_object_classes.htm – Isaac
沒關係,所以它會在時間段內出現,並用'+'代替它。但是如果它不在正確的路徑上,並且它不會調用其他子例程(如果success = false),它將不會將其全部退回。 – user2871898