繁体   English   中英

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

[英]Solving a maze using recursion in java from file

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

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

}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM