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 ...
随机推荐
- CSS的基本认识
1.定义: 级联样式表(Cascading Style Sheet)简称“CSS”,通常又称为“风格样式表(Style Sheet)”,它是用来进行网页风格设计的. 2.对CSS的基本认识: CSS是 ...
- MyBatis魔法堂:Insert操作详解
一.前言 数据库操作怎能少了INSERT操作呢?下面记录MyBatis关于INSERT操作的笔记,以便日后查阅. 二. insert元素 属性详解 其属性如下: parameterType:入参的全限 ...
- js原型和原型链个人理解(我的理解)
<script> //普通对象与函数对象,js万物皆是对象 //自带的 function a1() { function f1() {} var f2=function () {} var ...
- [Cycle.js] Making our toy DOM Driver more flexible
Our previous toy DOM Driver is still primitive. We are only able to sends strings as the textContent ...
- iOS模拟器分辨率的问题(转载)
转载地址:http://justsee.iteye.com/blog/2123545 不积跬步 无以至千里 不积小流 无以成江海 博客 微博 相册 收藏 留言 关于我 ios8/sdk ...
- Ubantu下编译Linux Kernel
wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.9.3.tar.gztar -xzf linux-3.9.3.tar.gzcd li ...
- ASP.NET网页抓取数据
我的数据通过一个TextBox输入,这些代码是写在一个button的点击事件里的. 网页数据抓取大概分为两步,第一步是获取网页源代码: 具体注释如下: var currentUrl = TextBox ...
- 关于IE11
最近,一个开发代号为Windows Blue的Windows操作系统泄漏到了互联网上,该操作系统的内置浏览器为IE11,本文将介绍一下这个泄漏版的IE11中有哪些关键的新变化和新特性. 预先声明: 本 ...
- arraylist与List<>
arraylist 不用规定 类型 list<>用规定类型 推荐list 这个编译检查类型 出错率低 代码质量高
- sql 表名为关键字
user在sql server中时一个关键字,如上面说所的,有时候我们无意中将其作为表的名称,当我们在sql语句中要使用该名称时例如:select *from user这是会提示user附近有语法错误 ...