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

最新文章

  1. Vimium使用快捷键总结
  2. Linux系统中如何挂载第二块硬盘
  3. Java五道输出易错题解析(避免小错误)
  4. 引用POPUI来实现弹窗效果,且弹窗中的内容可以点击事件
  5. C99新特性:变长数组(VLA)
  6. poj 3295 Tautology
  7. ORACLE 单实例完全卸载数据库
  8. PHP获取本周开始时间
  9. Android采用Application总结一下
  10. C# WinForm 拖动无边框窗体 改变无边框窗体尺寸
  11. HDU 1863 Kruskal求最小生成树
  12. Cocos2d-x中的CC_CALLBACK_X详解
  13. TWaver 2D+GIS+3D的试用和在线Demo
  14. batch 常用命令
  15. Aras简单报表
  16. 技术Leader相关文章和思考
  17. js的页面传值cookie.session
  18. POJ 3087 模拟
  19. Go 标准库 —— sync.Mutex 互斥锁
  20. “i词汇”宣传文案

热门文章

  1. HBase命令(一) -- 库操作
  2. display : -webkit-box-inline 我见
  3. Linux启动管理:grub
  4. webpack构建与loaders
  5. git checkout -b 的详细讲解
  6. LYDSY模拟赛day3 涂色游戏
  7. Mac Pro 安装 Sublime Text 2.0.2,个性化设置,主题 和 插件 收藏
  8. nginx的那些内置变量
  9. post 之checkbox 未被选中解决方法
  10. redis-string1