DFS迷宫发生器

时间:2012-11-01 17:52:02

标签: java

我正在使用深度优先搜索来制作迷宫生成程序。我有以下代码,但我无法弄清楚我做错了什么。我试图让两者之间的X说出哪些是墙。但是它会覆盖它们,并将路径放在彼此旁边。任何帮助都会很棒。以下是输出。

. . . # # # # # # # 
. . . . # . . . . . 
. . . . # . # # # . 
. . # . # . . . # . 
. . # # # # # . # # 
. . . . # . . . # # 
. # # . # . # # # # 
. # # . . . # # # # 
. . . . . # # # # # 
# # # # # # # # # # 

import java.util.Random;

public class DepthFirstSearch {

    private Stack stack;
    public DepthFirstSearch() {}
    public char[][] DFS (int size, char[][] maze) {

        Random myRand = new Random();
        stack = new Stack(size);
        int x = myRand.nextInt(size);
        while (x % 2 == 0)
            x = myRand.nextInt(size);
        int y = myRand.nextInt(size);
        while (y % 2 == 0)
            y = myRand.nextInt(size);

        maze[x][y] = ' ';
        int total = (size * size) / 4;
        int visited = 1;
        int random[] = new int[4];
        int totalrand;

        while (visited < total) {
            totalrand = 0;
            if (x > 1 && maze[x - 2][y] == 'X')
                random[totalrand++] = 1;
            if (x < size - 2 && maze[x + 2][y] == 'X')
                random[totalrand++] = 2;
            if (y > 1 && maze[x][y - 2] == 'X')
                random[totalrand++] = 3;
            if (y < size - 2 && maze[x][y + 2] == 'X')
                random[totalrand++] = 4;

            if (totalrand > 0) {
                switch(random[myRand.nextInt(totalrand)]) {
                    case 1: maze[x-2][y] = maze[x-1][y] = ' ';
                            x -= 2;
                            stack.push(x * size + y);
                            visited++;
                            break;
                    case 2: maze[x+2][y] = maze[x+1][y] = ' ';
                            x += 2;
                            stack.push(x * size + y);
                            visited++;
                            break;
                    case 3: maze[x][y-2] = maze[x][y-1] = ' ';
                            y -= 2;
                            stack.push(x * size + y);
                            visited++;
                            break;
                    case 4: maze[x][y+2] = maze[x][y+1] = ' ';
                            y += 2;
                            stack.push(x * size + y);
                            visited++;
                            break;
                }
            }
            else {
                int vert = stack.pop();
                x = vert / size;
                y = vert % size;
            }
        }
        return maze;
    }

}

1 个答案:

答案 0 :(得分:1)

事实证明,这完美无缺。我试图提高堆栈的效率,并搞砸了。因此,使用工作堆栈,它可以很好地工作。