迷宫求解器java

时间:2015-04-16 01:38:39

标签: java maze solver

我需要为APCS编写迷宫解决程序,该程序涉及基于文本的1和0矩阵。我必须编写一个代码,找到一个路径,如果有的话,从坐标0,0到右边的任何地方。这是我到目前为止所拥有的

public class Maze {
    private int[][] maze;
    private int sizes = 0;
    private boolean[][] checked;

    public Maze(int size, String line) {
        checked = new boolean[size][size];
        sizes = size;
        out.println(sizes - 1);
        Scanner joe = new Scanner(line);
        maze = new int[size][size];
        for (int x = 0; x < size; x++) {
            for (int y = 0; y < size; y++) {
                maze[x][y] = joe.nextInt();
            }
        }
    }

    public boolean hasExitPath(int r, int c) {
        boolean solved = false;
        boolean wall = false;

        if (r == sizes - 1) {
            solved = true;
            return solved;
        }

        maze[r][c] = 2;
        if (maze[r + 1][c] == 1) {
            out.println("down");
            hasExitPath(r + 1, c);
        }else if (maze[r][c + 1] == 1) {
            out.println("left");
            hasExitPath(r, c + 1);
        }else if (maze[r - 1][c] == 1) {
            out.println("up");
            hasExitPath(r - 1, c);
        }else if (maze[r][c - 1] == 1) {
            out.println("right");
            hasExitPath(r, c - 1);
        }
        System.out.println(r + " " + c);
        return solved;
    }

    public String toString() {
        String output = "";
        for (int y = 0; y < sizes; y++) {
            for (int x = 0; x < sizes; x++) {
                output = output + maze[y][x];
            }
            output = output + "\n";
        }
        return output;
    }
}

这是主要的课程

public class MazeRunner {
    public static void main(String args[]) throws IOException {
        Scanner mazeinfo = new Scanner(new File("maze.dat"));

        int size = mazeinfo.nextInt();
        mazeinfo.nextLine();
        String b = mazeinfo.nextLine();
        Maze m = new Maze(size, b);
        out.println(m);
        out.println(m.hasExitPath(0, 0));

        size = mazeinfo.nextInt();
        mazeinfo.nextLine();
        b = mazeinfo.nextLine();
        m = new Maze(size, b);
        out.println(m);
        out.println(m.hasExitPath(0, 0));

        size = mazeinfo.nextInt();
        mazeinfo.nextLine();
        b = mazeinfo.nextLine();
        m = new Maze(size, b);
        out.println(m);
        out.println(m.hasExitPath(0, 0));

        size = mazeinfo.nextInt();
        mazeinfo.nextLine();
        b = mazeinfo.nextLine();
        m = new Maze(size, b);
        out.println(m);
        out.println(m.hasExitPath(0, 0));

        size = mazeinfo.nextInt();
        mazeinfo.nextLine();
        b = mazeinfo.nextLine();
        m = new Maze(size, b);
        out.println(m);
        out.println(m.hasExitPath(0, 0));

        size = mazeinfo.nextInt();
        mazeinfo.nextLine();
        b = mazeinfo.nextLine();
        m = new Maze(size, b);
        out.println(m);
        out.println(m.hasExitPath(0, 0));
    }
}

以下是需要解决的迷宫图像

https://drive.google.com/file/d/0BzE3Cu7SjRlNdzRHYjM4UzZkY00/view?usp=sharing

我在hasExitPath方法中添加了一堆调试代码,以帮助我理解发生了什么。每当我运行程序时,它似乎无法追踪迷宫。我需要在此程序中添加什么内容?

2 个答案:

答案 0 :(得分:3)

调用hasExitPath(r , c)将始终返回false,除非r == size - 1为真。由于您从r == 0开始,size > 0为真,因此代码将始终以false生成。使用

if(hasExitPath(r + 1, c))
     return true;

而不是简单地调用hasExitPath(r + 1, c);来解决这个问题(对于hasExitPath(r , c)的所有其他递归调用都是如此)。

答案 1 :(得分:0)

我反对递归方法,如果迷宫变得足够大,你可能会出现堆栈溢出问题。我建议你做的是根据你旅行的方向进行操作,并根据你到达十字路口或死路一条重新评估方向。我可以给你一些链接到一些代码,这些代码可以解决迷宫问题但不使用二维数组。