java 迷宫算法

public class MM{
    public static void main(String[] args){
            int[][] map=new int[8][7];
            for(int i=0;i<map.length;i++){
                    map[i][0]=1;
                    map[i][6]=1;
                    map[3][1]=1;
                    map[3][2]=1;
                    map[6][4]=1;
                    map[2][4]=1;
                    map[3][4]=1;
                    map[4][4]=1;
                    map[5][4]=1;
                for(int j=0;j<map[0].length;j++){
                    map[0][j]=1;
                    map[7][j]=1;
                }
            }
            Map m=new Map();
            m.setWay(map,1,1);

            for (int i = 0; i < map.length; i++) {
                for (int j =0 ; j < map[0].length; j++) {
                    System.out.printf("%d\t",map[i][j]);
                }
                    System.out.println();
            }
    }
}

class Map{
    public static int n=0;
        public static boolean setWay(int[][] arr,int i,int j){


                if(arr[6][5]==2)return true;
                else{
                    if(arr[i][j]==0){
                        arr[i][j]=2;
                            n++;
                            if(setWay(arr,i+1,j)){
                                return true;
                            }else if(setWay(arr,i,j+1)){
                                return true;
                            }else if(setWay(arr,i-1,j)){
                                return true;
                            }else if(setWay(arr,i,j-1)){
                                return true;
                            }else{
                                n--;
                                arr[i][j]=3;
                                return false;
                            }
                    }else{
                        return false;
                    }
                }
        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成迷宫算法有很多种,其中比较常见的是深度优先搜索(DFS)和广度优先搜索(BFS)。 下面是使用DFS算法生成迷宫Java代码示例: ```java import java.util.*; public class MazeGenerator { private int width; private int height; private int[][] maze; private boolean[][] visited; public MazeGenerator(int width, int height) { this.width = width; this.height = height; this.maze = new int[2 * width + 1][2 * height + 1]; this.visited = new boolean[width][height]; } public void generate() { // 初始化迷宫,所有格子都是墙壁 for (int i = 0; i < 2 * width + 1; i++) { for (int j = 0; j < 2 * height + 1; j++) { maze[i][j] = 1; } } // 从起点开始生成迷宫 dfs(0, 0); // 将迷宫的外围设置为墙壁 for (int i = 0; i < 2 * width + 1; i++) { maze[i][0] = maze[i][2 * height] = 1; } for (int i = 0; i < 2 * height + 1; i++) { maze[0][i] = maze[2 * width][i] = 1; } } public int[][] getMaze() { return maze; } private void dfs(int x, int y) { visited[x][y] = true; // 随机打乱四个方向的顺序 List<Integer> directions = Arrays.asList(0, 1, 2, 3); Collections.shuffle(directions); for (int direction : directions) { int dx = 0, dy = 0; switch (direction) { case 0: // 上 if (y == 0 || visited[x][y - 1]) { continue; } dy = -1; break; case 1: // 右 if (x == width - 1 || visited[x + 1][y]) { continue; } dx = 1; break; case 2: // 下 if (y == height - 1 || visited[x][y + 1]) { continue; } dy = 1; break; case 3: // 左 if (x == 0 || visited[x - 1][y]) { continue; } dx = -1; break; } // 将当前格子和相邻格子之间的墙壁打通 maze[2 * x + 1 + dx][2 * y + 1 + dy] = 0; dfs(x + dx, y + dy); } } } ``` 使用方法: ```java MazeGenerator generator = new MazeGenerator(10, 10); generator.generate(); int[][] maze = generator.getMaze(); ``` 其中,`width`和`height`表示迷宫的宽度和高度,`maze`是一个二维数组,表示生成的迷宫,其中`0`表示通路,`1`表示墙壁。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值