Walls and Gates 解答
Question
You are given a m x n 2D grid initialized with these three possible values.
-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.
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
Solution
Problems like "shortest distance/ nearest neighbor" can be always solved by BFS.
distance from a specific point (x, y) to one exit (x', y') = distance from (x', y') to (x, y)
So we start BFS on exit points.
Af first, I tried BFS on every exit point one by one, but the time consuming is huge and reports "Time Limit Exceed".
One key point is that we can add all exit points at first, and do BFS once. Time complexity O(mn).
For BFS, don't forget visited[][] array.
public class Solution {
private final int[][] directions = {{0,1},{0,-1},{1,0},{-1,0}};
public void wallsAndGates(int[][] rooms) {
if (rooms == null || rooms.length < 1) {
return;
}
int m = rooms.length, n = rooms[0].length;
int distance = 0;
Deque<int[]> queue = new LinkedList<int[]>();
boolean[][] visited = new boolean[m][n];
// Add all exit points to queue
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (rooms[i][j] == 0) {
queue.add(new int[]{i, j});
}
}
}
// BFS
while (!queue.isEmpty()) {
distance++;
int size = queue.size();
for (int i = 0; i < size; i++) {
int[] prev = queue.poll();
for (int k = 0; k < 4; k++) {
int newX = prev[0] + directions[k][0];
int newY = prev[1] + directions[k][1];
if (newX < 0 || newX >= rooms.length || newY < 0 || newY >= rooms[0].length) {
continue;
}
if (rooms[newX][newY] == -1) {
continue;
}
// !!! check visisted
if (visited[newX][newY]) {
continue;
}
visited[newX][newY] = true;
if (rooms[newX][newY] > distance) {
rooms[newX][newY] = distance;
}
queue.offer(new int[]{newX, newY});
}
}
}
}
}
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 ...
- 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 ...
- [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 ...
随机推荐
- nmap 使用脚本引擎进行扫描
1.下载nmap(nmap官网). 2.安装nmap. 3.编辑环境变量(windows下所需),保存.
- Linux备份
Eking<longpeisky@vip.qq.com> 19:35:17 增量备份是针对于上一次备份(无论是哪种备份):备份上一次备份后,所有发生变化的文件. (增量备份过程中,只备份 ...
- ASP.NET 实现PDF文件下载
本文介绍了一种在ASP.NET中下载文件的方法. 方法一:可能是最简单的.最短的方式: Response.ContentType = "application/pdf"; Resp ...
- python 间谍程序传输文件 socket编程
本程序实现了,把目标机器的某个目录(可控)的所有的某种类型文件(可控)全部获取并传到己方的机器上. 1.用了base64的encode(infile,outfile)加密,以及decode(infil ...
- jQuery.extend 和 jQuery.fn.extend
1.jQuery.extend 我们先把jQuery看成了一个类,这样好理解一些.jQuery.extend(),是扩展的jQuery这个类. 假设我们把jQuery这个类看成是人类,能吃饭能喝水能跑 ...
- Javascript进阶篇——(DOM—节点---插入、删除和替换元素、创建元素、创建文本节点)—笔记整理
插入节点appendChild()在指定节点的最后一个子节点列表之后添加一个新的子节点.语法: appendChild(newnode) //参数: //newnode:指定追加的节点. 为ul添加一 ...
- discuzx3.2伪静态
首先,我们要新建一个名为.htaccess的文件,文件名为空白,这点很重要.很多人无法新建这个文件,在这里教大家如何新建没有名字的文件. 新建一个TXT文本,名字先默认.然后打开这个文本,然后把我们的 ...
- 货币小写转大写.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- (一)Activity参数传递
1.主Activity,用于启动另一个Activity()public class MainActivity extends Activity { @Override protected void o ...
- C#之获取本地IP地址
最近协助一个项目解决了一个获取IP地址的问题,手机客户端与WebService进行通讯,然后WebService通过TCP通讯把指令传递到另一台PC机上.在测试的过程中,总是会出现WebService ...