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 ...
随机推荐
- day52
今天依旧 阅读2篇的整理 数学试卷一套 综合练习计算 政治视频一个 范帅长难句一个 翻译两句 作文大小各一个 linux基础 今天折腾最久的应该是linux了 自己重新手动安装了一下 遇到的不少问题在 ...
- WPF - 如何引用external dll中图片
前几天一直想引用别的DLL里面的一个图片.总是出现各种各样问题,一气之下将图片拷贝到当前Project中,运行良好.虽然知道引用图片,就1.2.列出来的2点就够了. 1. The Build Acti ...
- User Defined Runtime Attributes
设置View borderWidth/cornerRadius/borderColor 为了兼容CALayer 的KVC ,你得给CALayer增加一个分类 CALayer+BorderColor.h ...
- hdu 4756 Install Air Conditioning
非正规做法,一个一个的暴,减一下枝,还得采用sort,qsort居然过不了…… #include <cstdio> #include <cmath> #include < ...
- spring Scurity终于测试OK了,复杂的功能还待深入研究!发布出来一起探讨吧!
spring Scurity终于测试OK了,复杂的功能还待深入研究!发布出来一起探讨吧! 就是因 为研究它,我的个天啦!头都大了一圈!还待修改完整版!我的目标不是每个项目拿到它就能使用!到时再说啦.. ...
- HDU 4760 Good FireWall 完好Trie题解
本题乍看像是线段树之类的区间操作,只是由于仅仅是须要查找ip的前缀,故此事实上是使用Trie来做. 挺高难度的Trie应用,做完这道题之后说明Trie功力有一定火候了. 这里的Trie使用到了Dele ...
- Gunplot 命令大全
在linux命令提示符下运行gnuplot命令启动,输入quit或q或exit退出. plot命令 gnuplot> plot sin(x) with line linetype 3 linew ...
- Linux多任务编程——进程
进程编程常用函数 1--- fork pitd_t fork(void); 创建一个新的子进程,其父进程为调用 fork() 函数的进程: 返回值:成功:子进程返回 0,父进程返回 子进程 PID:失 ...
- 关于sys、system、sysman等在EM中登录的问题
1.sysman要先在SQL*Plus上解锁: (1)以"sysdba"的身份登录 conn system/密码 as sysdba; (2)解锁 alter user sysma ...
- Linux基础知识笔记
1.case的命令格式 #!/bin/sh echo "please input number 1 to 3" read number case $number in ) e ...