使用DFS解决迷宫时出现问题

时间:2018-04-03 16:52:36

标签: java algorithm recursion depth-first-search maze

我正在做一个创建迷宫然后解决它的任务, 我将迷宫模拟成阵列, 用1代表墙,0 - 路,5 - 开始,9 - 目的地 如果已使用道路,则将其值更改为2; 它适用于创建迷宫, 在大多数情况下,它可以找到目的地的路径,但有时它仍会弹出错误,说明#34;空堆栈错误",如图所示......

[enter image description here]

我无法找到问题,你可以帮助我吗?

package MazeDFS;

import java.util.Random;
import java.util.Stack;

public class maze {

    public static void main(String[] args) {
        //create maze
        //and solve it
        //define the size as 9*9
            int r,c;
            r = 9;
            c = 9;
            int[][] newMaze = mazeGenerator(r,c);
            newMaze[1][1] = 5;
            mazePrinter(newMaze,r,c);
            Stack<Step> path = new Stack<Step>();
            Step s0 = new Step(1,1);
            path.push(s0);
            long startTime = System.nanoTime();
            newMaze = solve(newMaze, 1, 1, path);
            long endTime = System.nanoTime();
            System.out.println("Time length:" + (endTime - startTime));


    }

    public static int[][] mazeGenerator(int x, int y){
        Random randNum = new Random();
        /*I simulated the maze as a x*y array
        Values of the array represent elements of the maze
        0 -- road, 1 -- wall, 5 -- start, 9 -- destination
        */
        int[][] maze = new int[x][y];
        for(int i = 0; i < x; i++) {
            for(int j = 0; j < y; j++) {
                //bound of the maze
                if(i==0 || j==0 || i==x-1 || j==y-1) {
                    maze[i][j] = 1;
                }
                //build walls
                if(j%2 == 0) {
                    maze[i][j] = 1;
                }
            }
        }
        //randomly choose a row of every wall and make it a road
        for(int i = 1; i < y-1; i++) {
            int row = randNum.nextInt(x-2)+1;
            maze[row][i] = 0;
        }
        maze[x-2][y-2] = 9;
        return maze;
    }

    public static void mazePrinter(int[][] maze,int x, int y) {
        for(int i=0; i<x; i++) {
            for(int j=0; j<y; j++) {
                System.out.print(maze[i][j]);
            }
            System.out.println();
        }
    }

    public static int[][] solve(int[][] newMaze, int x, int y, Stack<Step> path) {
        mazePrinter(newMaze, 9,9);
        System.out.println();
        if(newMaze[x][y] == 9) {
            Step s = new Step(x,y);
            newMaze[x][y] = 2;
            path.push(s);
            return newMaze;
        }
        //going down
        int dx = 1;
        int dy = 0;
        if(newMaze[x+dx][y+dy] == 0 || newMaze[x+dx][y+dy] == 9) {
            //if visited mark as 2
            if(newMaze[x+dx][y+dy] != 9) {
                newMaze[x+dx][y+dy] = 2;
            }
            Step ss = new Step(x+dx,y+dy);
            path.push(ss);
            System.out.println("going down");
            return solve(newMaze, x+dx, y+dy, path);
        }
        //going up
        dx = -1;
        dy = 0;
        if(newMaze[x+dx][y+dy] == 0 || newMaze[x+dx][y+dy] == 9) {
            //if visited mark as 2
            if(newMaze[x+dx][y+dy] != 9) {
                newMaze[x+dx][y+dy] = 2;
            }
            Step ss = new Step(x+dx,y+dy);
            path.push(ss);
            System.out.println("going up");
            return solve(newMaze, x+dx, y+dy, path);
        }
        //turn right
        dx = 0;
        dy = 1;
        if(newMaze[x+dx][y+dy] == 0 || newMaze[x+dx][y+dy] == 9) {
            //if visited mark as 2
            if(newMaze[x+dx][y+dy] != 9) {
                newMaze[x+dx][y+dy] = 2;
            }
            Step ss = new Step(x+dx,y+dy);
            path.push(ss);
            System.out.println("turn right");
            return solve(newMaze, x+dx, y+dy, path);
        }
        //turn left
        dx = 0;
        dy = -1;
        if(newMaze[x+dx][y+dy] == 0 || newMaze[x+dx][y+dy] == 9) {
            //if visited mark as 2
            if(newMaze[x+dx][y+dy] != 9) {
                newMaze[x+dx][y+dy] = 2;
            }
            Step ss = new Step(x+dx,y+dy);
            path.push(ss);
            System.out.println("turn left");
            return solve(newMaze, x+dx, y+dy, path);
        }
        //if their is no way to go then turn back to the last step
        Step sss = path.pop();
        int x1 = sss.getX();
        int y1 = sss.getY();
        System.out.println("return to:"+"x1:"+x +", y1:"+ y);   
        return solve(newMaze, x1, y1, path);
    }
}

class Step{
    private int x;
    private int y;
    Step(int x, int y){
        this.x = x;
        this.y = y;
    }
    public int getX() {
        return this.x;
    }
    public int getY() {
        return this.y;
    }

}

0 个答案:

没有答案