走迷宫(广度优先搜索(BFS))

题目描述

给定一个 N×MN×M 的网格迷宫 GG。GG 的每个格子要么是道路,要么是障碍物(道路用 11 表示,障碍物用 00 表示)。

已知迷宫的入口位置为 (x1,y1)(x1​,y1​),出口位置为 (x2,y2)(x2​,y2​)。问从入口走到出口,最少要走多少个格子。

输入描述

输入第 11 行包含两个正整数 N,MN,M,分别表示迷宫的大小。

接下来输入一个 N×MN×M 的矩阵。若 Gi,j=1Gi,j​=1 表示其为道路,否则表示其为障碍物。

最后一行输入四个整数 x1,y1,x2,y2x1​,y1​,x2​,y2​,表示入口的位置和出口的位置。

1≤N,M≤1021≤N,M≤102,0≤Gi,j≤10≤Gi,j​≤1,1≤x1,x2≤N1≤x1​,x2​≤N,1≤y1,y2≤M1≤y1​,y2​≤M。

输出描述

输出仅一行,包含一个整数表示答案。

若无法从入口到出口,则输出 −1−1。

输入输出样例

示例 1

输入

5 5 
1 0 1 1 0
1 1 0 1 1 
0 1 0 1 1
1 1 1 1 1
1 0 0 0 1
1 1 5 5 

输出

8

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Scanner;
    
    class Pair{
        int x;
        int y;
        int steps;
    
        public Pair(int x,int y,int steps) {
            this.x=x;
            this.y=y;
            this.steps=steps;
        }
    }
    
    public class Main {
        public static int findShortest(int[][] dp,int x1,int y1,int x2,int y2){
            int n= dp.length;  //迷宫的行数
            int m=dp[0].length;  //迷宫的列数
            boolean[][] visited=new boolean[n+2][m+2];
            Queue<Pair>queue=new LinkedList<>();
            /*
            存储Pair类型对象的队列,Queue是一个接口,
            通常用于表示先进先出(FIFO)的数据结构
            这里使用LinkedList作为队列的实现,是因为它可以方便地进行队列的操作,
            如入队(add或offer)和出队(remove或poll)
            */
            queue.add(new Pair(x1,y1,0));
            visited[x1][y1]=true;
            int[][] directions={{0,1},{1,0},{0,-1},{-1,0}};
            while (!queue.isEmpty()){
                Pair curr=queue.poll();  //poll()是队列(Queue)接口的一个方法,从队列中移除并返回头部元素。如果队列为空,它会返回null
                if(curr.x==x2&&curr.y==y2){
                    return curr.steps;
                }
                for(int[] dir:directions){
                    int newX=curr.x+dir[0];
                    int newY=curr.y+dir[1];
                    if(newX >= 1 && newX <= n && newY >= 1 && newY <= m && dp[newX][newY]==1 && !visited[newX][newY]){
                        queue.add(new Pair(newX,newY, curr.steps+1));
                        visited[newX][newY]=true;
                    }
                }
            }
            return -1;
        }
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int N=scan.nextInt();
            int M=scan.nextInt();
            int[][] dp=new int[N+2][M+2];
            for(int i=1;i<=N;i++){
                for(int j=1;j<=M;j++){
                    dp[i][j]= scan.nextInt();
                }
            }
            int x1= scan.nextInt();
            int y1= scan.nextInt();
            int x2= scan.nextInt();
            int y2= scan.nextInt();
            System.out.println(findShortest(dp,x1,y1,x2,y2));
            scan.close();
        }
    }
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值