[LeetCode] 286. Walls and Gates

You are given an m x n grid rooms 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 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 1:

Input: rooms = [[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]]

Example 2:

Input: rooms = [[-1]]
Output: [[-1]]

Constraints:

  • m == rooms.length
  • n == rooms[i].length
  • 1 <= m, n <= 250
  • rooms[i][j] is -10, or 231 - 1.

墙与门。

你被给定一个 m × n 的二维网格 rooms ,网格中有以下三种可能的初始化值:

-1 表示墙或是障碍物
0 表示一扇门
INF 无限表示一个空的房间。然后,我们用 231 - 1 = 2147483647 代表 INF。你可以认为通往门的距离总是小于 2147483647 的。
你要给每个空房间位上填上该房间到 最近门的距离 ,如果无法到达门,则填 INF 即可。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/walls-and-gates
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意是给一个二维矩阵,里面的 -1 代表墙,0 代表门,INF 代表一个空的房间。请改写所有的 INF,表明每个 INF 到最近的门的距离。

这一题是带障碍物的 flood fill类 的题目,既然是问最短距离,所以这个题目应该还是偏 BFS 做。还有一题思路比较接近的是542题,也是在矩阵内通过已知的一些距离去累加起来找未知坐标的距离。还有一道题也比较类似,也是带障碍物的 flood fill 类型的题目,1730题

具体思路是我们从矩阵中的门出发,也就是先找到矩阵中的 0,把这些 0 的坐标放入 queue。从 queue 中弹出这些 0 的时候,往四个方向扫描,看看这些 0 的周围是否有 INF。如果有,则这些 INF 就有一个具体的距离了,把这些有了距离的 INF 再放入 queue。这些 INF 有了具体的距离之后,别的距离门更远的 INF 也就有机会被计算出具体的距离了。

时间O(mn)

空间O(mn)

Java实现

 1 class Solution {
 2     public void wallsAndGates(int[][] rooms) {
 3         // corner case
 4         if (rooms == null || rooms.length == 0) {
 5             return;
 6         }
 7 
 8         // normal case
 9         Queue<int[]> queue = new LinkedList<>();
10         for (int i = 0; i < rooms.length; i++) {
11             for (int j = 0; j < rooms[0].length; j++) {
12                 if (rooms[i][j] == 0) {
13                     queue.offer(new int[] { i, j });
14                 }
15             }
16         }
17         while (!queue.isEmpty()) {
18             int[] cur = queue.poll();
19             int r = cur[0];
20             int c = cur[1];
21             if (r > 0 && rooms[r - 1][c] == Integer.MAX_VALUE) {
22                 rooms[r - 1][c] = rooms[r][c] + 1;
23                 queue.offer(new int[] { r - 1, c });
24             }
25             if (c > 0 && rooms[r][c - 1] == Integer.MAX_VALUE) {
26                 rooms[r][c - 1] = rooms[r][c] + 1;
27                 queue.offer(new int[] { r, c - 1 });
28             }
29             if (r < rooms.length - 1 && rooms[r + 1][c] == Integer.MAX_VALUE) {
30                 rooms[r + 1][c] = rooms[r][c] + 1;
31                 queue.offer(new int[] { r + 1, c });
32             }
33             if (c < rooms[0].length - 1 && rooms[r][c + 1] == Integer.MAX_VALUE) {
34                 rooms[r][c + 1] = rooms[r][c] + 1;
35                 queue.offer(new int[] { r, c + 1 });
36             }
37         }
38     }
39 }

 

二刷再提供一个代码量略短的BFS实现,时间空间复杂度一样。

 1 class Solution {
 2     public void wallsAndGates(int[][] rooms) {
 3         // corner case
 4         if (rooms == null || rooms.length == 0) {
 5             return;
 6         }
 7         
 8         // normal case
 9         int m = rooms.length;
10         int n = rooms[0].length;
11         Queue<int[]> queue = new LinkedList<>();
12         for (int i = 0; i < m; i++) {
13             for (int j = 0; j < n; j++) {
14                 if (rooms[i][j] == 0) {
15                     queue.offer(new int[] { i, j });
16                 }
17             }
18         }
19         
20         int[] dx = {-1, 1, 0, 0};
21         int[] dy = {0, 0, -1, 1};
22         while (!queue.isEmpty()) {
23             int[] cur = queue.poll();
24             int x = cur[0];
25             int y = cur[1];
26             for (int k = 0; k < 4; k++) {
27                 int newX = x + dx[k];
28                 int newY = y + dy[k];
29                 if (newX >= 0 && newX < m && newY >= 0 && newY < n && rooms[newX][newY] == Integer.MAX_VALUE) {
30                     rooms[newX][newY] = rooms[x][y] + 1;
31                     queue.offer(new int[] { newX, newY });
32                 }
33             }
34         }
35     }
36 }

 

相关题目

286. Walls and Gates

542. 01 Matrix

1730. Shortest Path to Get Food

flood fill题型总结

LeetCode 题目总结

posted @ 2020-05-02 09:58  CNoodle  阅读(276)  评论(0编辑  收藏  举报