Решение лабиринта с использованием рекурсии в Java из файла - PullRequest
0 голосов
/ 21 октября 2019

Я пытаюсь решить лабиринт с помощью рекурсии, но после внесенных мною изменений решенный лабиринт никогда не выйдет в итоге, и я понятия не имею, почему это так. Я был бы признателен, если бы вы, ребята, могли попытаться отладить эту проблему, потому что я понятия не имею, с чего начать.

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

1 Ответ

0 голосов
/ 21 октября 2019

Исключение возникает, когда вы пытаетесь получить доступ к элементам массива без добавления какой-либо проверки или привязки к индексу, к которому вы обращаетесь.

Это происходит в следующих строках кода:

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];

Нет проверки, чтобы убедиться, что столбец -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();
    }

}
}
...