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

标签 java file recursion maze

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

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/

相关文章:

java - SLF4J 自定义绑定(bind)不起作用

java - Jenkins 无法为我的 Selenium 项目编译 Maven

c - 如何从c中的文件中读取日期

c++ - 大量函数递归 - C++

c - 返回后的函数 kepps(递归 C)

Java GUI 与 Arraylist

java - neo4j QueryResult不会退出

jquery - 如何使用 jquery 输入类型文件过滤器分离事件

linux - 如何找出目录或文件所在的挂载/分区? (Linux 服务器)

java - 终于在 stackoverflow 之前在 java 中工作,在 main() 中使用递归循环