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

Understand the problem:
It is very classic backtracking problem. We can start from each gate (0 point), and searching for its neighbors. We can either use DFS or BFS solution.

 public class Solution {
public void wallsAndGates(int[][] rooms) {
if (rooms == null || rooms.length == ) return;
int m = rooms.length, n = rooms[].length; for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (rooms[i][j] == ) {
helper(i, j, , rooms);
}
}
}
} private void helper(int row, int col, int distance, int[][] rooms) {
int rows = rooms.length, cols = rooms[].length;
if (row < || row >= rows || col < || col >= cols || rooms[row][col] == -) return;
if (distance > rooms[row][col]) return;
if (distance < rooms[row][col]) {
rooms[row][col] = distance;
}
helper(row - , col, distance + , rooms);
helper(row + , col, distance + , rooms);
helper(row, col - , distance + , rooms);
helper(row, col + , distance + , rooms);
}
}

BFS

对于bfs,因为我们是把所有的0点在最开始的时候加入到了queue里面,所以,当其中一个0点访问到一个空点的时候,那么我们一定可以说那是最短距离。所以,时间复杂度上,bfs比dfs好很多。

 public class Solution {
public void wallsAndGates(int[][] rooms) {
Queue<int[]> queue = new LinkedList<int[]>();
int rows = rooms.length;
if (rows == ) {
return;
}
int cols = rooms[].length; // 找出所有BFS的起始点
for (int i = ; i < rows; i++) {
for (int j = ; j < cols; j++) {
if (rooms[i][j] == ) {
queue.offer(new int[]{i, j});
}
}
} // 定义下一步的位置
int[][] dirs = {{-, }, {, }, {, -}, {, }}; // 开始BFS
while (!queue.isEmpty()) {
int[] top = queue.poll();
for (int k = ; k < dirs.length; k++) {
int x = top[] + dirs[k][];
int y = top[] + dirs[k][];
if (x >= && x < rows && y >= && y < cols && rooms[x][y] == Integer.MAX_VALUE) {
rooms[x][y] = rooms[top[]][top[]] + ;
queue.add(new int[]{x, y});
}
}
}
}
}

From:

http://buttercola.blogspot.com/2015/09/leetcode-walls-and-gates.html

https://segmentfault.com/a/1190000004184488

Walls and Gates的更多相关文章

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

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

  4. LeetCode Walls and Gates

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

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

  6. Walls and Gates 解答

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

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

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

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

随机推荐

  1. IdentityDbContext

    Move the ApplicationUser definition to your DAL. Inherit your MyDbContext from IdentityDbContext< ...

  2. testNg vs junit 4.X @Test

    http://www.ibm.com/developerworks/cn/java/j-cq08296/ 一个简单的测试用例 初看起来,JUnit 4 和 TestNG 中实现的测试非常相似.为了更好 ...

  3. Python之路【第七篇续】:进程、线程、协程

    Socket Server模块 SocketServer内部使用 IO多路复用 以及 “多线程” 和 “多进程” ,从而实现并发处理多个客户端请求的Socket服务端.即:每个客户端请求连接到服务器时 ...

  4. http模拟请求工具

    http模拟请求工具: postman(chrome应用) Request Maker(chrome插件) Request Maker(网站:http://www.requestmaker.com/) ...

  5. JavaScript内置对象与原型继承

    (一)   理解JavaScript类定义 1>关于内置对象理解 console.log(Date.prototype.__proto__===Object.prototype    //tru ...

  6. gvim如何显示html属性代码提示? vim 如何显示 javascript属性及方法提示?

    gvim如何显示html属性代码 可以在vim中 显示 html, css, js等的属性/方法 提示: 一是: 在 ~/.vim/after/syntax/ 目录中 安装 css-color.vim ...

  7. connect() failed (111: Connection refused) while connecting to upstream

    配置好lamp后,在浏览器中运行程序后,出现上面的错误. 转自:http://www.xuejiehome.com/blread-1828.html I'm experiencing 502 gate ...

  8. smarty string_format用法 取小数点后2位

    <{if $d.ul_pv}> <{$d.sum/$d.ul_pv|string_format:'%.2f'}> <{else}> 0.00 <{/if}&g ...

  9. CF456C Boredom (DP)

    Boredom CF#260 div2 C. Boredom Codeforces Round #260 C. Boredom time limit per test 1 second memory ...

  10. 从Google开源RE2库学习到的C++测试方案

    最近因为科研需求,一直在研究Google的开源RE2库(正则表达式识别库),库源码体积庞大,用C++写的,对于我这个以前专供Java的人来说真的是一件很痛苦的事,每天只能啃一点点.今天研究了下里面用到 ...