Walls and Gates

https://www.lintcode.com/problem/walls-and-gates/description

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 value 2^31 - 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, that room should remain filled with INF

Example

Example1

Input:
[[2147483647,-1,0,2147483647],[2147483647,2147483647,2147483647,-1],[2147483647,-1,2147483647,-1],[0,-1,2147483647,2147483647]]
Output:
[[3,-1,0,1],[2,2,1,-1],[1,-1,2,-1],[0,-1,3,4]]

Explanation:
the 2D grid is:
INF  -1  0  INF
INF INF INF  -1
INF  -1 INF  -1
  0  -1 INF INF
the answer is:
  3  -1   0   1
  2   2   1  -1
  1  -1   2  -1
  0  -1   3   4

Example2

Thoughts

由0,-1,INF组成的二维矩阵,-1代表不能通过,每个INF更新成离它最近0的距离。最短距离而且又是『显式』图,用BFS。问『0->1』最短,要从『1->0』做BFS,有两个原因: 1. 一般1的数目要<<0,所需处理的结点更少;2. 如果是只找一个最近的1,从所有的1开始一起遍历,每次遇到的没被标记过的0一定是被最近的1(之一)访问着,这个0就可以被标记以后无需再访问。因此这道题把所有标为0的作为BFS初始点,做BFS。

Code

Analysis

做题耗时15min 时间复杂度O(MN).

Last updated

Was this helpful?