[抄题]:

您将获得一个使用这三个可能值初始化的 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

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

  1. 不知道“距离”应该对应什么数学表达式:没有抓住题目的特点,求“最短的”距离,就可以联想到最短路问题
  2. 在最短路问题中,以前没见过:把看似复杂的多原点-单终点 反向转化为 单原点- 多终点 增加超级源之后,多原点-多终点也能最终转换成单原点-多终点

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 新被淹没了的x y坐标记得放到队列中

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

  1. 新被淹没了的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最近的出口的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. LeetCode 286. Walls and Gates

    原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...

  5. 【LeetCode】286. Walls and Gates 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  6. <DFS & BFS> 286 339 (BFS)364

    286. Walls and Gates DFS: 思路是,搜索0的位置,每找到一个0,以其周围四个相邻点为起点,开始 DFS 遍历,并带入深度值1,如果遇到的值大于当前深度值,将位置值赋为当前深度值 ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. LeetCode题目按公司分类

    LinkedIn(39) 1 Two Sum 23.0% Easy 21 Merge Two Sorted Lists 35.4% Easy 23 Merge k Sorted Lists 23.3% ...

  9. [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 ...

随机推荐

  1. c++下为使用pimpl方法的类编写高效的swap函数

    swap函数是c++中一个常用的函数,用于交换两对象的值,此外还用于在重载赋值运算符中处理自赋值情况和进行异常安全性编程(见下篇),标准模板库中swap的典型实现如下: namespace stl { ...

  2. Ubuntu网络配置IP和DNS等,适用于14.04,16.04和17.10

    本文主要介绍Ubuntu系统的网络设置,包括IP,DNS和主机名等,适用于14.04,16.04和17.10等版本 ===============  完美的分割线 ================ = ...

  3. 响应: 500 OOPS: priv_sock_get_int 错误: 读取目录列表失败

    /************************************************************************* * 响应: 500 OOPS: priv_sock ...

  4. 第2本MATLAB书

    前面看的<DSP using MATLAB>后面还有两章,其内容太难了,看不下去,暂时放放: 因为工作中需要MATLAB和电磁场的相关知识,从网上找了本 初步翻了翻,里面有代码有图片,英文 ...

  5. MyBatis_Study_003(字段名与属性名称不一致,resultMap)

    源码:https://github.com/carryLess/mbtsstd-003 1.主配置文件 <?xml version="1.0" encoding=" ...

  6. juqery学习3之juqery对象条件筛选

    代码例子:某个div块下的字体样式的控制. //script代码 <script src="${sitePath}/cec_wcp/js/jquery-1.8.2.min.js&quo ...

  7. appium使用

    学过selenium的朋友再来看appium,基本上就是一个环境折腾问题,还有一个就是初始化Driver的问题,以下代码是初始化Driver WebDriver driver = null; // 驱 ...

  8. ballerina 学习二十 http/https

    提供http && https server && client 访问功能 client endpoint 说白了就是http client 参考代码 import b ...

  9. Android实现带图标的ListView

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/bear_huangzhen/article/details/23991119 Android实现带图 ...

  10. python2用pip进行安装时报错Fatal error in launcher: Unable to create process using '"'

    win7下python3和python2共存环境 用pip安装一个包执行pip2 install xxx的时候报错Fatal error in launcher: Unable to create p ...