286 walls and gate最近的出口
[抄题]:
您将获得一个使用这三个可能值初始化的 m×n 2D 网格。-1 - 墙壁或障碍物。 0 - 门。 INF - Infinity是一个空房间。我们使用值 2 ^ 31 - 1 = 2147483647 来表示INF,您可以假设到门的距离小于 2147483647。
在代表每个空房间的网格中填入到距离最近门的距离。如果不可能到达门口,则应填入 INF。
给定 2D 网格:
INF -1 0 INF
INF INF INF -1
INF -1 INF -1
0 -1 INF INF
返回结果:
3 -1 0 1
2 2 1 -1
1 -1 2 -1
0 -1 3 4
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
- 不知道“距离”应该对应什么数学表达式:没有抓住题目的特点,求“最短的”距离,就可以联想到最短路问题
- 在最短路问题中,以前没见过:把看似复杂的多原点-单终点 反向转化为 单原点- 多终点 增加超级源之后,多原点-多终点也能最终转换成单原点-多终点
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 新被淹没了的x y坐标记得放到队列中
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 新被淹没了的x y坐标记得放到队列中
[复杂度]:Time complexity: O(m*n) 每个点都被淹没了Space complexity: O(m+n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
棋盘图:用图上的BFS
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
public class Solution {
/*
* @param rooms: m x n 2D grid
* @return: nothing
*/
//declare
int n, m;
static final int INF = 2147483647;
public void wallsAndGates(int[][] rooms) {
n = rooms.length;
if (n == 0) {
return ;
}
m = rooms[0].length;
Queue<Integer> qx = new LinkedList<>();
Queue<Integer> qy = new LinkedList<>();
int[] dx = {0, 1, 0, -1};
int[] dy = {1, 0, -1, 0};
//get all 0 into queue
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (rooms[i][j] == 0) {
qx.offer(i);
qy.offer(j);
}
}
}
while (!qx.isEmpty()) {
int cx = qx.poll();
int cy = qy.poll();
for (int i = 0; i < 4; i++) {
int nx = cx + dx[i];
int ny = cy + dy[i];
if (0 <= nx && nx < n && 0 <= ny && ny < m &&
rooms[nx][ny] == INF) {
qx.offer(nx);
qy.offer(ny);
rooms[nx][ny] = rooms[cx][cy] + 1;//watered so shoud be put
}
}
}
}
}
286 walls and gate最近的出口的更多相关文章
- [LeetCode] 286. Walls and Gates 墙和门
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- 286. Walls and Gates
题目: You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an ob ...
- [LeetCode] 286. Walls and Gates_Medium tag: BFS
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- LeetCode 286. Walls and Gates
原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...
- 【LeetCode】286. Walls and Gates 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- <DFS & BFS> 286 339 (BFS)364
286. Walls and Gates DFS: 思路是,搜索0的位置,每找到一个0,以其周围四个相邻点为起点,开始 DFS 遍历,并带入深度值1,如果遇到的值大于当前深度值,将位置值赋为当前深度值 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode题目按公司分类
LinkedIn(39) 1 Two Sum 23.0% Easy 21 Merge Two Sorted Lists 35.4% Easy 23 Merge k Sorted Lists 23.3% ...
- [LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS
In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...
随机推荐
- spring-security-4 (3)spring security过滤器的创建与注册原理
spring security是通过一个过滤器链来保护你的web应用安全.在spring security中,该过滤链的名称为springSecurityFilterChain,类型为FilterCh ...
- TypeScript学习笔记(八) - 声明文件
本篇将介绍TypeScript的声明文件,并简单演示一下如何编写和使用声明文件.本篇也是这个系列的最后一篇. 一.声明文件简介 TypeScript作为JavaScript的超集,在开发过程中不可避免 ...
- NUnit的Attribute使用详解
NUNIT使用详解(一) 2008/08/26 11:40 NUnit是一个单元测试框架,专门针对于.NET来写的,它是是xUnit的一员.NUnit完全由C#语言来编写,并且编写时充分利用了许多.N ...
- linux-一篇文章完成lnmp环境的编译安装
lnmp环境搭建 前置条件 操作系统安装:CentOS 6.8 64位最小化安装. 配置好IP.DNS.网关.主机名 配置防火墙,开启80.3306端口 关闭访问墙 service iptables ...
- Linux常用命令总结--不断补充
首先介绍一个很有用的命令:history 查看linux机器上历史命令. 在Linux下查看内存我们一般用free命令:free -m 查看硬盘状况:df -h 查看cpu信息:less /proc ...
- Hystrix已经停止开发,官方推荐替代项目Resilience4j
随着微服务的流行,熔断作为其中一项很重要的技术也广为人知.当微服务的运行质量低于某个临界值时,启动熔断机制,暂停微服务调用一段时间,以保障后端的微服务不会因为持续过负荷而宕机.本文介绍了新一代熔断器R ...
- 基于ffmpeg静态库的应用开发
最近几天在试着做基本ffmpeg静态库的开发,只有main中包含了avdevice_register_all 或avfilter_register_all,编译就通不过,undefined refre ...
- 使用ntp从时间同步服务器更新centos系统时间的方法
CentOS系统时间同步的步骤如下: 复制代码 代码如下: cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtimentpdate us.pool.ntp ...
- pfsense openvpn上网终于通了
先看配置,等会在说过程中遇到的问题: 1.openvpn配置: /var/etc/openvpn/server2.conf下: port 1195 proto tcp dev tap writepid ...
- HBuilder webApp开发 Websql增删改查操作
来源:http://blog.csdn.net/zhuming3834/article/details/51471434 这段时间公司要求我们做原生iOS和安卓的都转做H5开发APP,使用的工具HBu ...