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 than 2147483647.

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.

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 这个题目是用BFS来解决, 最naive approach就是扫每个点,然后针对每个点用BFS一旦找到0, 代替点的值. 时间复杂度为 O((m*n)^2);
我们还是用BFS的思路, 但是我们先把2D arr 扫一遍, 然后把是0的位置都append进入queue里面, 因为是BFS, 所以每一层最靠近0的位置都会被替换, 并且标记为visited, 然后一直BFS到最后即可.
时间复杂度降为O(m*n) 1. Constraints
1) empty or len(rooms[0]) == 0, edge case
2) size of the rooms < inf
3) 每个元素的值为0, -1, inf 2. Ideas BFS T: O(m*n) S: O(m*n) 1) edge case,empty or len(rooms[0]) == 0 => return
2) scan 2D arr, 将值为0 的append进入queue里面
3) BFS, 如果是not visited过得, 并且 != 0 or -1, 更改值为dis + 1, 另外append进入queue, add进入visited 3. Code
 class Solution:
def wallAndGates(self, rooms):
if not rooms or len(rooms[0]) == 0: return
queue, lr, lc, visited, dirs = collections.deque(), len(rooms), len(rooms[0]), set(), [(1, 0), (-1, 0), (0, 1), (0, -1)]
for i in range(lr):
for j in range(lc):
if rooms[i][j] == 0:
queue.append((i, j, 0))
visited.add((i, j)) while queue:
pr, pc, dis = queue.popleft()
for d1, d2 in dirs:
nr, nc = pr + d1, pc + d2
if 0<= nr < lr and 0<= nc < lc and (nr, nc) not in visited and rooms[nr][nc] != -1:
rooms[nr][nc] = dis + 1
queue.append((nr,nc, dis + 1))
visited.add((nr, nc))

4. Test cases

1) edge case

2)

INF  -1  0  INF
INF INF INF -1
INF -1 INF -1
0 -1 INF INF

After running function, the 2D grid should be:

  3  -1   0   1
2 2 1 -1
1 -1 2 -1
0 -1 3 4

[LeetCode] 286. Walls and Gates_Medium tag: BFS的更多相关文章

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

  2. [LeetCode] 127. Word Ladder _Medium tag: BFS

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  3. LeetCode 286. Walls and Gates

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

  4. [LeetCode] 101. Symmetric Tree_ Easy tag: BFS

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  5. [LeetCode] 133. Clone Graph_ Medium tag: BFS, DFS

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  6. [LeetCode] 310. Minimum Height Trees_Medium tag: BFS

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  7. [LeetCode] 301. Remove Invalid Parentheses_Hard tag:BFS

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  8. [LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  9. [LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

随机推荐

  1. centos 安装 Vagrant

    使用的软件: 1. CentOS:  CentOS release 6.4 (Final) 2. Vagrant: vagrant_1.2.2_i686.rpm 3. Virtualbox: Virt ...

  2. 原生js--cookie操作的封装

    封装cookie的操作:查询cookie个数.查询所有cookie的键.获取cookie.设置cookie.删除cookie.清除全部cookie /** * cookieStorage */func ...

  3. JavaScript怎样学

    嘿,我最近接到一个 Web 项目,不过老实说,我这两年没怎么接触 Web 编程,听说 Web 技术已经发生了一些变化.听说你是这里对新技术最了解的 Web 开发工程师? 准确地说,我是一名「前端工程师 ...

  4. [Tjoi2016&Heoi2016]排序[01序列]

    4552: [Tjoi2016&Heoi2016]排序 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 994  Solved: 546[Sub ...

  5. 使用nginx做反代时遇到413 Request Entity Too Large的解决方法

    在使用nginx做反向代理的时候,被反代的系统在上传文件的时候遇到413 错误 :Request Entity Too Large 原因是nginx限制了上传文件的大小,在nginx中可以配置最大允许 ...

  6. 改变presentModalView大小

    rc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; rc.modalPresentationStyle = UIModalP ...

  7. AD PCB中两个不同高度器件重叠 软件报警告变绿

    这个问题遇到几次了,每次都要在网上搜索解决方法,今天记下来! 在规则里面不检查器件高度这项应该是最简单,也不影响其他规则的方法了! 具体操作: Design - rules - Component C ...

  8. Jquery操作select选项集合,判断集合中是否存在option

    转载:http://www.cnblogs.com/pepcod/archive/2012/07/03/JavaScript.html Query获取Select选择的Text和Value: 语法解释 ...

  9. html如何给图片加角标

    https://segmentfault.com/q/1010000006551803?_ea=1074082 <html><style> #a1 { width: 200px ...

  10. mui+vue+photoclip做APP头像裁剪上传

    做APP由于项目需要,需要做用户头像上传的功能,头像上传包括拍照和相册选择图片进行上传,这里使用的技术是mui封装的plus,在进行图片裁剪的时候,使用的是photoclip来进行裁剪,由于个人在使用 ...