0
我有這個任務,我應該在Java中創建一個迷宮求解器。我決定應用的算法按以下方式工作:它是一種遞歸方法,在每次找到路徑時再次調用自身。如果它運行到死衚衕,它會調用第二個遞歸方法「goBack」,它會一直返回,直到它找到一個新路徑。牆是0,路徑是1,走路是2,走過兩次的路徑是3s。這個想法很簡單,但我無法實現。 ArrayOutOfBounds異常一直顯示。有人對此有任何想法嗎?Java遞歸迷宮
public class Project5v2 {
static String mazecsv = "/Users/amorimph/Documents/COMP 182/Project 5/mazeinput.csv";
static File solvedMaze = new File("/Users/amorimph/Documents/COMP 182/Project 5/solvedMaze.txt");
static int[][] maze = new int[50][50];
static int trigger = 0;
static int mazeWidth;
static int mazeHeight;
public static void main(String[] args) {
readCSV(mazecsv);
start(maze);
mazeToString(maze);
}
public static void readCSV(String csvfile) {
BufferedReader br = null;
String line = "";
String csvSplitBy = ",";
int x = 1;
int y = 0;
try {
br = new BufferedReader(new FileReader(csvfile));
br.readLine();
while ((line = br.readLine()) != null) {
String[] info = line.split(csvSplitBy);
for (x = 1; x < info.length; x++) {
maze[y][x] = Integer.parseInt(info[x]);
}
mazeWidth = info.length;
y++;
mazeHeight = y;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void start(int[][] maze) {
int i = 0;
while(maze[0][i] != 1) {
i++;
}
System.out.println(i);
move(maze,i,1);
}
public static void move(int[][] maze, int x, int y) {
for (int i = 0; i < 4; i++) {
switch(i) {
case 0: if(maze[x][y-1] == 1) {
maze[x][y] = 2;
maze[x][y-1] = 2;
move(maze, x, y-1);
break;
}
case 1: if(maze[x-1][y] == 1) {
maze[x][y] = 2;
maze[x-1][y] = 2;
move(maze, x-1, y);
break;
}
case 2: if(maze[x+1][y] == 1) {
maze[x][y] = 2;
maze[x+1][y] = 2;
move(maze, x+1, y);
break;
}
case 3: if(maze[x][y+1] == 1) {
maze[x][y] = 2;
maze[x][y+1] = 2;
move(maze, x, y+1);
break;
}
//case 4:
// maze[x][y] = 2;
// goBack(maze, y, x);
// break;
}
}
}
public static void goBack(int[][] maze, int x, int y) {
for (int i = 0; i < 7; i++) {
switch(i) {
case 0: if(maze[x][y-1] == 1) {
maze[x][y] = 2;
maze[x][y-1] = 2;
move(maze, x, y-1);
break;
}
case 1: if(maze[x-1][y] == 1) {
maze[x][y] = 2;
maze[x-1][y] = 2;
move(maze, x-1, y);
break;
}
case 2: if(maze[x+1][y] == 1) {
maze[x][y] = 2;
maze[x+1][y] = 2;
move(maze, x+1, y);
break;
}
case 3: if(maze[x][y+1] == 1) {
maze[x][y] = 2;
maze[x][y+1] = 2;
move(maze, x, y+1);
break;
}
case 4: if(maze[x][y+1] == 2) {
maze[x][y] = 3;
goBack(maze, x, y+1);
break;
}
case 5: if(maze[x+1][y] == 2) {
maze[x][y] = 3;
goBack(maze, x+1, y);
break;
}
case 6: if(maze[x-1][y] == 2) {
maze[x][y] = 3;
goBack(maze, x-1, y);
break;
}
case 7: if(maze[x][y-1] == 2) {
maze[x][y] = 3;
goBack(maze, x, y-1);
break;
}
}
}
}
public static void FWriter(String content, File file) {
try {
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void mazeToString(int[][] maze) {
for(int i = 0; i < mazeHeight; i++) {
for(int j = 0; j < mazeWidth; j++) {
FWriter(Integer.toString(maze[i][j]), solvedMaze);
}
FWriter("\n", solvedMaze);
}
}
}
我知道迷宮是由我們的邊界的正方形組成的,如果邊界是有顏色的,那麼它是一堵牆,不能被穿過? – Alexey