原题链接在这里:https://leetcode.com/problems/walls-and-gates/

题目:

You are given a m x n 2D grid initialized with these three possible values.

  1. -1 - A wall or an obstacle.
  2. 0 - A gate.
  3. INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than2147483647.

Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.

For example, given the 2D grid:

INF  -1  0  INF
INF INF INF -1
INF -1 INF -1
0 -1 INF INF

After running your function, the 2D grid should be:

  3  -1   0   1
2 2 1 -1
1 -1 2 -1
0 -1 3 4

题解:

BFS, 先把所有gate加到que中。对于每一个从que中poll出来的gate,看四个方向是否有room, 若有,把room的值改正gate + 1, 在放回到que中.

Time Complexity: O(m*n). m = rooms.length, n = rooms[0].length. 每个点没有扫描超过两遍. Space: O(m*n).

AC Java:

 class Solution {
public void wallsAndGates(int[][] rooms) {
if(rooms == null || rooms.length == 0 || rooms[0].length == 0){
return;
} int m = rooms.length;
int n = rooms[0].length;
LinkedList<int []> que = new LinkedList<>();
int level = 1;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(rooms[i][j] == 0){
que.add(new int[]{i, j});
}
}
} int [][] dirs = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; while(!que.isEmpty()){
int size = que.size();
while(size-- > 0){
int [] cur = que.poll();
for(int [] dir : dirs){
int x = cur[0] + dir[0];
int y = cur[1] + dir[1];
if(x < 0 || x >= m || y < 0 || y >= n || rooms[x][y] != Integer.MAX_VALUE){
continue;
} que.add(new int[]{x, y});
rooms[x][y] = level;
}
} level++;
}
}
}

跟上Robot Room CleanerRotting OrangesShortest Distance from All Buildings.

LeetCode Walls and Gates的更多相关文章

  1. [LeetCode] 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. leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings

    542. 01 Matrix https://www.cnblogs.com/grandyang/p/6602288.html 将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值. 最 ...

  3. [Locked] Walls and Gates

    Walls and Gates You are given a m x n 2D grid initialized with these three possible values. -1 - A w ...

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

  5. LeetCode 286. Walls and Gates

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

  6. Walls and Gates -- LeetCode

    You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...

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

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

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

  9. [Swift]LeetCode286. 墙和门 $ Walls and Gates

    You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...

随机推荐

  1. linux 安装程序

    tar文件打开 tar -xvf myfile.tar bz2文件打开

  2. 学习编写Windows Live Writer插件

    1.参考链接 参考:http://www.cnblogs.com/liulun/archive/2009/05/27/1491116.html Live Writer API参考:http://msd ...

  3. 最新版EJS的include函数已支持参数传递

    最新版的express中partial函数已经被移除,使用include虽然可以实现同样的效果,但是代码看起来很不爽比如 1 <%-partial("user/home",{ ...

  4. font 和 text 字和文本操作

  5. QtCreator下运行opencv出现realloc():pointer invalid

    解决办法是将qmake换成4.8的,qmake5.2的支持opencv支持的不是很好

  6. ObjectContext,DataContext和DBContext 分别获取linq 的sql方法

    ObjectContext 先定义一个扩展方法: public static string ToTraceString<T>(this IQueryable<T> t) { s ...

  7. nodeJS中exports和mopdule.exports的区别

    每一个node.js执行文件,都自动创建一个module对象,同时,module对象会创建一个叫exports的属性,初始化的值是 {} module.exports = {}; Node.js为了方 ...

  8. 每日英語2013.09.09(Email常用單字)

    carbon copy 副本抄送 forward 轉交;轉寄(+to) Please check the file carried by this email for the details.細節請看 ...

  9. 六、雪花《苹果iOS实例编程入门教程》

    该app为应用的功能为制作一场雪景 现版本 SDK 8.4 Xcode 纲要:- UIImageView 的运用- onTimer 代码运用- onAnimation 代码运用 运行Xcode 选择 ...

  10. cookie的设置、获取和删除封装

    在我们为了去完成数据储存,有时会用到cookie,简单封装一下cookie: <!DOCTYPE html> <html lang="en"> <he ...