BFS迷宫Java模板(附例题)

模板

迷宫问题主要使用BFS来做,因为BFS循环得到的第一个答案就是最短路径,下面给出这种走迷宫的模板,像求从起点走到终点所使用的最小步数,或者输出他按照怎么路径走出迷宫的都可以用这个模板

下面这个模板写的是最简单的走迷宫,给定一个迷宫,0是障碍物,1是可以行走的,然后问从左上角走到右下角的最小步数。后面的例题也是和最简单的迷宫问题类似,就是又加了其他一些特殊的限制条件。

import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;

public class 迷宫模板 {
    //创建节点,我在这里使用静态类的方法创建,也可以直接创建一个类
    static class pair{
        int x,y; //x、y分别代表行和列
        //为该类创建构造构造方法
        public pair(int x, int y){
            this.x = x;
            this.y = y;
        }
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt(); //迷宫的行
        int M = scan.nextInt(); // 迷宫的列
        //输入迷宫
        int[][] maze = new int[N][M];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                maze[i][j] = scan.nextInt();
            }
        }
        int[][] dir ={{0,1},{1,0},{0,-1},{-1,0}};
        int[][] meet = new int[N][M]; //这个数组用来标记是否走过这个点,当然也可用boolean布尔数组来标记。
        Deque<pair> queue = new LinkedList<>();
        pair  start =new pair(0,0);  //创建一个开始结点
        queue.offer(start);
        meet[0][0] = 1; //标记该开始结点已经走过了
        int ans = 0;  // 用来记录走了多少步
        boolean flag = false;  //判断是否找到终点退出循环用
        while(!queue.isEmpty()){
            int size = queue.size(); //用来记录
            ans++;    // 向外围扩展一圈,ans就加1,直到找到终点,得到的答案就是走的步数
            while(size>0) {
                size--;  
                pair a = queue.poll();
                for (int i = 0; i < 4; i++) {
                    int newrow = a.x + dir[i][0];
                    int newcol = a.y + dir[i][1];
                    if (newrow < 0 || newcol < 0 || newrow >= N || newcol >= M || meet[newrow][newcol] == 1 || maze[newrow][newcol] == 0)  continue;
                    pair a2 = new pair(newrow, newcol);
                    meet[newrow][newcol] = 1;
                    queue.offer(a2);
                    if(newrow ==(N-1) && newcol ==(M-1)) {
                        flag = true; //标记已经找到终点
                        break;
                    }
                }
                if (flag) break;
            }
            if (flag) break;
        }
        System.out.println(flag?ans:-1);
    }
}

例题1、走迷宫

【蓝桥杯模板题传送门
在这里插入图片描述
在这里插入图片描述

这个题就和上面给的模板非常相似,仅仅是改变了起点和终点,这里要特别注意的是输入起点和终点的时候要-1 其他的就和上面那个类似了。

import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;

public class 走迷宫 {
    static class pair{
        int x;
        int y;
         public pair(int x,int y){
             this.x = x;
             this.y = y;
         }
        public boolean equals(pair obj) {
            return x == obj.x && y == obj.y;
        }
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        int M = scan.nextInt();
        int[][] map =new int[N][M];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                map[i][j] = scan.nextInt();
            }
        }
        int[][] meet = new int[N][M];
        pair start =new pair(scan.nextInt()-1,scan.nextInt()-1);
        pair end = new pair(scan.nextInt()-1,scan.nextInt()-1);
        int[][] dir ={{0,1},{1,0},{0,-1},{-1,0}};
        Deque<pair> k =new LinkedList<>();
        k.offer(start);
        meet[start.x][start.y] = 1;
        int ans = 0;
        boolean  flag= false;
        while(!k.isEmpty()){
            int size = k.size();
            ans++;
            while(size>0){
                size--;
                pair a =k.poll();
            for(int i=0; i<4;i++){
               int newrol = a.x + dir[i][0];
               int newcol = a.y + dir[i][1];
               if(newrol<0 || newcol<0 || newrol >=N || newcol>=M || meet[newrol][newcol]==1|| map[newrol][newcol]==0) continue;
                   pair a1 = new pair(newrol,newcol);
                   k.offer(a1);
                   meet[newrol][newcol] = 1;
                   if(end.equals(a1)) {
                       flag=true;
                       break;}

            }
            if(flag) break;
            }
            if(flag) break;
        }

        System.out.println(ans);
    }
}

例题2、迷宫

[蓝桥杯省赛填空题传送门]

在这里插入图片描述
本题迷宫
01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000

本题是求起点走到终点需要的最短路径,因为BFS一趟下来输出的就是最短路径,步数最少可能出现多种情况,同时也需要满足字典序最小,满足这个条件我们只需要按照字典序最小的顺序向四周扩展即可。但是本题要求的是最短路径,我们只需要在节点类里面新加入一个path属性,来记录走过的路径,最后走到终点以后输出path即可。

import java.util.Deque;
import java.util.LinkedList;

public class 迷宫填空 {
    static class pair{
        int x,y;
        String path;
        public pair(int x,int y, String path){
            super();
            this.x = x;
            this.y = y;
            this.path = path;
        }
    }
    public static void main(String[] args) {
    int[][] meet = new int[30][50];
    pair start = new pair(0,0,"");
    int[][] dir ={{1,0},{0,1},{0,-1},{-1,0}};
    String[] road ={"D","R","L","U"};
        Deque<pair> queue = new LinkedList<>();
        queue.offer(start);
    while(!queue.isEmpty()){
        pair p1 = queue.poll();
        for (int i = 0; i < 4; i++) {
           int  newrow = p1.x + dir[i][0];
           int newcol = p1.y + dir[i][1];
           if( newrow<0 || newcol<0 || newrow>=30 || newcol>=50||meet[newrow][newcol]==1 || map[newrow][newcol]==1) continue;
           String r1 = p1.path + road[i];
           pair a2 = new pair(newrow,newcol,r1);
           queue.offer(a2);
           meet[newrow][newcol] = 1;
           if(newrow==29&&newcol==49){
               System.out.println(a2.path);
           }

        }
    }
    }
    static int[][] map = {
            { 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0,
                    1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0 },
            { 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
                    0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1 },
            { 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0,
                    0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
            { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1,
                    0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1 },
            { 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0,
                    0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1,
                    0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
            { 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
                    1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
            { 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1,
                    0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0 },
            { 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
                    1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1 },
            { 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                    1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0 },
            { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0,
                    0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1 },
            { 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                    1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0 },
            { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0,
                    0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1 },
            { 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1,
                    1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0 },
            { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0,
                    0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1 },
            { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1,
                    0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
            { 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
                    1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1 },
            { 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1,
                    1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1 },
            { 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0,
                    0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0 },
            { 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1,
                    0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1 },
            { 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
                    1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1 },
            { 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1,
                    0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1 },
            { 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0,
                    1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0 },
            { 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0,
                    0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
            { 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1,
                    0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0 },
            { 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1,
                    1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1 },
            { 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1,
                    0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
            { 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1,
                    0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0 },
            { 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0,
                    0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1 },
            { 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1,
                    0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0 } };


}

总结 需要用到的一些队列知识

队列的相关知识
队列是一种特殊的线性表结构,具有先进先出(FIFO)的特点,他只能从队尾插入元素,在队的前端删除元素。
插入元素: offer() 方法和add()方法(推荐使用offer()因为当队列满的时候,add方法会返回异常,而offer方法只会放回false。)
删除元素: poll() 方法和remove()方法(推荐使用poll()方法,当队列是个空队列时,remove方法会返回异常而poll方法只会返回false
查看队首元素: peek()方法和element()方法 (区别: p两个虽然都能查询队列的头部元素,element()方法在队列为空是返回异常,而peek()方法返回null)

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,关于Java迷宫问题的BFS算法,我可以给您提供以下思路: 1. 首先需要定义一个二维数组来表示迷宫,0表示可以通过的路,1表示障碍物; 2. 定义一个队列来存储待访问的节点; 3. 将起点加入队列,并将其标记为已访问; 4. 对于队列中的每个节点,分别向上、下、左、右四个方向进行探索,如果该方向上的节点未被访问过且可以通过,则将其加入队列,并标记为已访问; 5. 直到队列为空或者找到终点为止。 以下是Java代码实现: ```java import java.util.LinkedList; import java.util.Queue; public class MazeBFS { public static int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 上下左右四个方向 public static boolean bfs(int[][] maze, int[] start, int[] dest) { int m = maze.length, n = maze[0].length; boolean[][] visited = new boolean[m][n]; // 标记是否访问过 Queue<int[]> queue = new LinkedList<>(); queue.offer(start); visited[start[0]][start[1]] = true; while (!queue.isEmpty()) { int[] cur = queue.poll(); if (cur[0] == dest[0] && cur[1] == dest[1]) { return true; } for (int[] dir : directions) { int x = cur[0] + dir[0], y = cur[1] + dir[1]; if (x >= 0 && x < m && y >= 0 && y < n && !visited[x][y] && maze[x][y] == 0) { queue.offer(new int[]{x, y}); visited[x][y] = true; } } } return false; } public static void main(String[] args) { int[][] maze = {{0, 0, 1, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 1, 0}, {1, 1, 0, 1, 1}, {0, 0, 0, 0, 0}}; int[] start = {0, 4}, dest = {4, 4}; System.out.println(bfs(maze, start, dest)); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦森森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值