Walls and Gates
-1- A wall or an obstacle.0- A gate.INF- Infinity means an empty room. We use the value231 - 1 = 2147483647to representINFas you may assume that the distance to a gate is less than2147483647.
INF.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
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的更多相关文章
- [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 ...
- 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的值. 最 ...
- [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 ...
- LeetCode Walls and Gates
原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...
- 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 ...
- Walls and Gates 解答
Question You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- owin
app.Properties["Hello"] = System.DateTime.Now; app.Run(async context => await context.R ...
- Java调试
线上load高的问题排查步骤是: 先用top找到耗资源的进程 ps+grep找到对应的java进程/线程 jstack分析哪些线程阻塞了,阻塞在哪里 jstat看看FullGC频率 jmap看看有没有 ...
- Apache日志配置详解(rotatelogs LogFormat)
logs/error_logCustomLog logs/access_log common--默认为以上部分 修改为如下: ErrorLog "|/usr/sbin/rotatelogs ...
- 浅谈JavaScript中的事件
引言 Html页面与JavaScript之间的交互是通过事件来完成的.事件,就是文档或者浏览器窗口中发生的一些特定的交互瞬间.可以使用侦听器(处理程序)来预订事件,以便事件发生时执行相应的代码.这在传 ...
- eclipse的html代码辅助失效解决办法
Eclipse IDE : .xhtml code assist is not working for JSF tag By mkyong | September 6, 2010 | Viewed : ...
- hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...
- Emacs配置文件
;;tab and space;;when true,emacs use mixture of tab and space to archieve(setq-default indent-tabs-m ...
- [译]git clean
git clean命令用来从你的工作目录中删除所有没有tracked过的文件. git clean经常和git reset --hard一起结合使用. 记住reset只影响被track过的文件, 所以 ...
- assign与weak区别(面试)
weak 比 assign 多了一个功能就是当属性所指向的对象消失的时候(也就是内存引用计数为0)会自动赋值为 nil ,这样再向 weak 修饰的属性发送消息就不会导致野指针操作crash. 可能不 ...
- TCP的流模式与UDP的报文模式对比
1 案例背景 在学习TCP-IP协议详解卷一时,读到介绍TCP协议的部分,发现TCP的首部是没有报文总长度字段的,而在UDP中是有的,对这个问题的思考引出了两者之间的区别. 2 案例 ...