gpt4 book ai didi

java - 使用java中的文件递归解决迷宫问题

转载 作者:行者123 更新时间:2023-12-01 22:41:54 25 4
gpt4 key购买 nike

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

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
#.#...

最佳答案

当您尝试访问数组元素而不对您正在访问的索引添加任何检查或绑定(bind)时,就会出现异常。

这发生在这些代码行上:

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 不为负数或 row+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();
}

}
}

关于java - 使用java中的文件递归解决迷宫问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58489855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com