递归除法迷宫Java

问题描述 投票:1回答:1

我在完成基于this link的算法时遇到问题。建造一堵墙后,我选择迷宫的上部或左侧,它似乎只能在需要中断递归并输入另一个divide方法调用的地方创建自己。我不确定我是否正确理解需要正确传递给divide方法的最后一次调用的值。

    public void divide(int x, int y, int width, int hight) {
        if (width< 2 || hight< 2) ;  

        else {
            boolean horizontal = chooseOrientation(width,hight);

            if (horizontal) {
                int randomNumber = r.nextInt(hight - 1);
                wallY = randomNumber + y;

                for (int i = x; i < width; i++) {                       
                    fields[wallY][i].setHorizontalWall();
                }
                fields[wallY][r.nextInt(width- 1)].deleteHorizontalWall();   
                hight = wallY - y + 1;
                divide(x, y, width, hight);
            }

            else {
                int randomNumber = r.nextInt(width- 1);
                WallX = randomNumber + x;

                for (int i = y; i < hight; i++) {
                    fields[i][WallX].setVerticalWall();
                }
                fields[r.nextInt(hight - 1) + y][WallX].deleteVerticalWall();
                width = WallX - x + 1;
               }

            if(horizontal){
                hight = y + hight + WallY-1;
                y = WallY + 1;
            }
            else {
                width = WallX - 1 + width + x;
                x = WallX + 1;
            }
            divide(x, y, width, hight);
        }
    }

java algorithm maze
1个回答
0
投票

在“递归除法”算法中,您从一个完整的二维网格图开始,然后开始去除水平和垂直“条纹”交替排列的边(=建筑物“墙”)。在每个“条带”中,只剩下一个边缘(“门”)。

此算法的基于图的版本可以在这里找到:

https://github.com/armin-reichert/mazes

https://github.com/armin-reichert/mazes/blob/master/mazes-algorithms/src/main/java/de/amr/maze/alg/others/RecursiveDivision.java

© www.soinside.com 2019 - 2024. All rights reserved.