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 ...
随机推荐
- 能取悦生理期的女性吗?Le Parcel提供女性卫生用品按月订购服务,不是按包出售而是可以按片自由搭配 | 36氪
能取悦生理期的女性吗?Le Parcel提供女性卫生用品按月订购服务,不是按包出售而是可以按片自由搭配 | 36氪 能取悦生理期的女性吗?Le Parcel提供女性卫生用品按月订购服务,不是按包出售而 ...
- Spring 的优秀工具类盘点第 2 部分
特殊字符转义 由于 Web 应用程序需要联合使用到多种语言,每种语言都包含一些特殊的字符,对于动态语言或标签式的语言而言,如果需要动态构造语言的内容时,一个我们经常会碰到的问题就是特殊字符转义的问题. ...
- 修改过mysql数据库字段内容默认值为当前时间
--添加CreateTime 设置默认时间 CURRENT_TIMESTAMP ALTER TABLE `table_name`ADD COLUMN `CreateTime` datetime N ...
- AssemblyInfo.cs文件的作用
在asp.net中有一个配置文件AssemblyInfo.cs主要用来设定生成的有关程序集的常规信息dll文件的一些參数,以下是默认的AssemblyInfo.cs文件的内容详细介绍 //是否符合公共 ...
- jre配置了1.6,但是eclipse仍然提示需要1.6以上的java
问题: eclipse创建web项目时,提示错误:Dynamic Web Module 3.0 requires Java 1.6 or newer. 原因: 这是因为当前的编译器java版本太低,请 ...
- Ubuntu 12.04设置打开远程桌面登录1
teamviewer_linux.deb sudo dpkg --install teamviewer_linux.deb
- Windows命令行(DOS命令)教程-5 (转载)http://arch.pconline.com.cn//pcedu/rookie/basic/10111/15325_4.html
5. copy copy在英文中是复制的意思 [功能] 复制一个或一组文件到指定的磁盘或目录中 [格式] copy [C:][path][filename.ext] [C:][path]filenam ...
- iOS — Autolayout之Masonry解读
前言 1 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-iphone3gs时 ...
- Light oj 1030 概率DP
D - Discovering Gold Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768 ...
- tttt
while(scanf("%d",&n)!=EOF) { res=-1; level(tmp,n,res,1); printf("%d/n",res); ...