力扣-图论-70【算法学习day.70】

前言

###我做这类文章一个重要的目的还是给正在学习的大家提供方向和记录学习过程(例如想要掌握基础用法,该刷哪些题?)我的解析也不会做的非常详细,只会提供思路和一些关键点,力扣上的大佬们的题解质量是非常非常高滴!!!


习题

1.迷宫中离入口最近的出路

题目链接: 1926. 迷宫中离入口最近的出口 - 力扣(LeetCode)

题面:

代码:

java 复制代码
class Solution {
    int[][] dirs = {{1,0},{-1,0},{0,1},{0,-1}};
    public int nearestExit(char[][] maze, int[] entrance) {
        int n = maze.length;
        int m = maze[0].length;
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(entrance);
        maze[entrance[0]][entrance[1]] = '+';
        int count = 0;
        while(queue.size()!=0){
            count++;
            int size = queue.size();
            for(int i = 0;i<size;i++){
                int[] arr =queue.poll();
                int x = arr[0];
                int y = arr[1];
                for(int[] brr:dirs){
                    int nx = x+brr[0];
                    int ny = y+brr[1];
                    if(nx>=0&&nx<=n-1&&ny>=0&&ny<=m-1&&maze[nx][ny]=='.'){
                        if(nx==0||nx==n-1||ny==0||ny==m-1)return count;
                        queue.offer(new int[]{nx,ny});
                         maze[nx][ny]='+';
                    }
                }
            }
        }
        return -1;
    }
}

后言

上面是力扣图论专题,下一篇是其他的习题,希望有所帮助,一同进步,共勉!

相关推荐
likuolei28 分钟前
XML 元素 vs. 属性
xml·java·开发语言
自不量力的A同学37 分钟前
Spring Boot 4.0.0 正式发布
java·spring boot·后端
d***29241 小时前
【spring】Spring事件监听器ApplicationListener的使用与源码分析
java·后端·spring
5***b971 小时前
解决报错net.sf.jsqlparser.statement.select.SelectBody
java
q***95221 小时前
Tomcat下载,安装,配置终极版(2024)
java·tomcat
2***d8851 小时前
详解tomcat中的jmx监控
java·tomcat
无敌最俊朗@1 小时前
Qt事件循环队列剖析!!!
java
9084869051 小时前
文旅业务相关前沿技术应用
学习·产品经理
v***5651 小时前
Spring Cloud Gateway 整合Spring Security
java·后端·spring
做怪小疯子1 小时前
LeetCode 热题 100——矩阵——旋转图像
算法·leetcode·矩阵