Question

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

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 解答的更多相关文章

  1. [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 ...

  2. 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的值. 最 ...

  3. [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 ...

  4. Walls and Gates

    You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...

  5. LeetCode Walls and Gates

    原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. thinkphp分页时修改last显示标题

    需要修改Page.class.php里lastSuffix为false,这样才能修改last显示标题. 然后就可以设置了 或者直接在方法中声明: $p->lastSuffix = false; ...

  2. Linux中open函数以及退出进程的函数

    open函数的flag详解1 读写权限:O_RDONLY O_WRONLY O_RDWR (1)linux中文件有读写权限,我们在open打开文件时也可以附带一定的权限说明 (譬如O_RDONLY就表 ...

  3. java 正则表达式获取值

    @Test public void testtest() { String test = "hahahhehe sendCode\":\"12367890123rsdfs ...

  4. 自写图片遮罩层放大功能jquery插件源代码,photobox.js 1.0版,不兼容IE6

    阿嚏~~~ 话说本屌丝没啥开发插件的经验,可是天公不作美,公司须要让我自己开发个图片放大的插件 但公司老大的话,宛如吾皇之圣旨,微臣必当肝脑涂地,莫敢不从啊~~~ 于是乎,作为一个超级小白,本人仅仅能 ...

  5. TTB 基本

    中文名 ,线程构建模块 外文名 Thread Building Blocks 缩    写 TBB 开    发 intel 目录 1线程构建模块 2黑体亮温 3斜交载重轮胎 4串联球轴承     1 ...

  6. 使用Qt Style Sheets制作UI特效

    引言 作为一套GUI框架,Qt是非常强大的.(注:Qt 不仅是一套优秀的GUI框架,同时也是一套出色的应用程序框架).在UI的制作方面Qt为广大开发者提供了一套强大而易用的工具,她就是——Qt Sty ...

  7. C语言的学习-基础知识点

    ---BOOL BOOL BOOL a = YES; printf("%d\n", a); a = NO; printf("%d", a); , b = ; B ...

  8. 对arm指令集的疑惑,静态库运行,编译报错等问题

    转载自http://www.jianshu.com/p/4a70aa03a4ea?utm_campaign=hugo&utm_medium=reader_share&utm_conte ...

  9. C#基本数据类型与C++区别

    与C++不同的地方: char占两个字节存Unicode字符, long long 改为 long ; unsize ... 改为 u... 新增: byte占1个字节,类似与C++char, sby ...

  10. gitolite随记

    1.git clone源码 git clone git://github.com/sitaramc/gitolite 2.安装 gitolite/install -ln 3.建立git仓库 gitol ...