从文件使用 java 中的递归求解迷宫



我正在尝试使用递归解决迷宫,但是在我进行编辑后,解决的迷宫最终永远不会输出,我不知道为什么会这样。 如果你们可以尝试调试这个问题,我将不胜感激,因为我不知道从哪里开始。

import java.util.*;
import java.io.*;
public class recursionMaze {
private char[][] Maze;
private int rows;
private int cols;
private int rowStart, colStart;
private String outputFilename;
public recursionMaze(String filename) throws IOException {
try {
this.outputFilename = filename;
Scanner reader = new Scanner(new File(filename));
StringBuilder sb = new StringBuilder();
while (reader.hasNext()) {
sb.append(reader.nextLine());
this.rows++;
}
this.cols = sb.length() / this.rows;
this.Maze = new char[this.rows][this.cols];
int m = 0;
System.out.println();
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
this.Maze[i][j] = sb.charAt(m++);
}
}
reader.close();
findStart();
Solve(this.rowStart, this.colStart);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("ERROR : " + e.getMessage());
}
}
private void findStart() {
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
if (Maze[i][j] == 'S') {
this.rowStart = i;
this.colStart = j;
}
}
}
}
private boolean Solve(int row, int col) {
char right = col + 1 < this.cols ? this.Maze[row][col + 1] : 'S';
char down = row + 1 < this.rows ? this.Maze[row + 1][col] : 'S';
char left = col - 1 >= 0 ? this.Maze[row][col - 1] : 'S';
char up = row - 1 >= 0 ? this.Maze[row - 1][col] : 'S';
if (right == 'G' || left == 'G' || up == 'G' || down == 'G') {
this.Maze[row][col] = '+';
try {
File file = new File(this.outputFilename + " solved");
PrintWriter writer = new PrintWriter(file);
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
writer.print(this.Maze[i][j]);
}
writer.println();
}
writer.close();
} catch (FileNotFoundException e) {
System.out.println("ERROR : " + e.getMessage());
}
return true;
}
boolean solved = false;
if (this.Maze[row][col] != 'S') {
this.Maze[row][col] = '+';
}
if (right == '.' && !solved) {
solved = Solve(row, col + 1);
}
if (down == '.' && !solved) {
solved = Solve(row + 1, col);
}
if (left == '.' && !solved) {
solved = Solve(row, col - 1);
}
if (up == '.' && !solved) {
solved = Solve(row - 1, col);
}
if (!solved) {
this.Maze[row][col] = '.';
}
return solved;
}
public static void main(String agrs[]) throws Exception {
try {
new recursionMaze("C:\Users\achtc\OneDrive\Desktop\Maze Folder\Maze5.txt");
System.out.println("File has been outputed.");
} catch (Exception e) {
System.out.println("ERROR : " + e.getMessage());
e.printStackTrace();
}
}
}

每当我跑步时,我都会得到这个

File has been outputed.

这是文本文件的外观

S...##
#.#...
#.## #
..#.##
#...#G
#.#...

当您尝试访问数组元素而不在正在访问的索引上添加任何检查或绑定时,异常即将到来。

这发生在以下代码行上:

char right = this.Maze[row][col + 1];
char down = this.Maze[row + 1][col];
char left = this.Maze[row][col - 1];
char up = this.Maze[row - 1][col];

没有检查以确保 col -1 不是负数或行 + 1 不超过实际行数等。

尝试使用这些 LOC (例如,实际逻辑将取决于您的问题陈述(:

char right = col+1 < this.cols ? this.Maze[row][col + 1] : 'S';
char down = row+1 < this.rows ? this.Maze[row + 1][col] : 'S';
char left = col-1 >=0 ? this.Maze[row][col - 1] : 'S';
char up =row-1 >=0 ?  this.Maze[row - 1][col]: 'S';

public class RecursionMaze {  
private static char[][] Maze = new char[20][20];
private int rows;
private int cols;
private int rowStart, colStart;
private String outputFilename;
public RecursionMaze(String filename) throws IOException {
try {
this.outputFilename = filename;
Scanner reader = new Scanner(new File(filename));
StringBuilder sb = new StringBuilder();
while (reader.hasNext()) {
sb.append(reader.nextLine());
this.rows++;
}
this.cols = sb.length() / this.rows;
this.Maze = new char[this.rows][this.cols];
int m = 0;
System.out.println();
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
this.Maze[i][j] = sb.charAt(m++);
}
}
reader.close();
findStart();
Solve(this.rowStart, this.colStart);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("ERROR : " + e.getMessage());
}
}
private void findStart() {
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
if (Maze[i][j] == 'S') {
this.rowStart = i;
this.colStart = j;
}
}
}
}
private boolean Solve(int row, int col) {
char right = col + 1 < this.cols ? this.Maze[row][col + 1] : 'S';
char down = row + 1 < this.rows ? this.Maze[row + 1][col] : 'S';
char left = col - 1 >= 0 ? this.Maze[row][col - 1] : 'S';
char up = row - 1 >= 0 ? this.Maze[row - 1][col] : 'S';
if (right == 'G' || left == 'G' || up == 'G' || down == 'G') {
this.Maze[row][col] = '+';
try {
File file = new File(this.outputFilename + " solved");
PrintWriter writer = new PrintWriter(file);
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
writer.print(this.Maze[i][j]);
}
writer.println();
}
writer.close();
} catch (FileNotFoundException e) {
System.out.println("ERROR : " + e.getMessage());
}
return true;
}
boolean solved = false;
if (this.Maze[row][col] != 'S') {
this.Maze[row][col] = '+';
}
if (right == '.' && !solved) {
solved = Solve(row, col + 1);
}
if (down == '.' && !solved) {
solved = Solve(row + 1, col);
}
if (left == '.' && !solved) {
solved = Solve(row, col - 1);
}
if (up == '.' && !solved) {
solved = Solve(row - 1, col);
}
if (!solved) {
this.Maze[row][col] = '.';
}
return solved;
}
public static void main(String agrs[]) throws Exception {
try {
new RecursionMaze("/Users/himani.agarwal/Documents/test100/host-service-user/src/main/resources/Maze.txt");
System.out.println("File has been outputed.");
} catch (Exception e) {
System.out.println("ERROR : " + e.getMessage());
e.printStackTrace();
}
}
}

最新更新