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. Yii2-admin RBAC权限管理的实现

    原文地址:http://www.open-open.com/lib/view/open1434638805348.html   http://wlzyan.blog.163.com/blog/stat ...

  2. c#之Redis队列

    摘要 这两天一直在考虑redis队列:一个生产者,多个消费者的情况,这里弄了一个demo进行测试. 一个例子 关于如何引用Redisclient 可以参考之前的这篇文章:c#之Redis实践list, ...

  3. Spring注入方式

  4. ScriptManager与UpdatePanel总结

    1.From http://www.cnblogs.com/Tim-Seven/archive/2011/02/11/1952409.html Ajax Extensions 2.ScriptMana ...

  5. StarWind的安装配置

    将安装包拷贝到本地后,执行以下exe文件,默认下一步,直到安装完成. 需汉化版本则将language_chinese.xml文件拷贝到以下路径: C:\Program Files\StarWind S ...

  6. 重读C#委托、事件有感

    去年学习C#的时候按照进度把C#的委托和事件“认真”的学习了一下,自己知道委托和事件的重要性,所以也努力的学习,可说实也没怎么学懂.碰巧这段时间在做一个解析GPS数据的小项目,因为其中有需要条件自动判 ...

  7. PHP的$_SERVER['PHP_SELF']造成的XSS漏洞攻击及其解决方案

    $_SERVER['PHP_SELF']简介 $_SERVER['PHP_SELF'] 表示当前 PHP文件相对于网站根目录的位置地址,与 document root 相关. 假设我们有如下网址,$_ ...

  8. linux之samba与linux权限

    当linux的文件夹或文件为用户或root时,则在window上共享出来的samba是不能进行修改的,当samba设置为 [share]path = /home#available = yes#bro ...

  9. php 调用系统命令

    system 与 exec 两者区别与联系:都会返回最后一行,命令执行成功的return返回值, 区别:system直接将输出内容echo出来,而exec将每一行输出内容保存到数组$output里. ...

  10. telnet: connect to address 127.0.0.1: Connection refused

    telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户提供了在本地计算机上完成远程主机工作的能力.在终端使用者的电脑上使用telnet程序,用它连接 ...