zoukankan      html  css  js  c++  java
  • [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: 

    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

     

    墙与门。

    题意是给一个二维矩阵,里面的 -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 }

    相关题目

    286. Walls and Gates

    542. 01 Matrix

    1730. Shortest Path to Get Food

    flood fill题型总结

    LeetCode 题目总结

  • 相关阅读:
    python3 TypeError: a bytes-like object is required, not 'str'
    Centos 安装Python Scrapy PhantomJS
    Linux alias
    Vim vimrc配置
    Windows下 Python Selenium PhantomJS 抓取网页并截图
    Linux sort
    Linux RSync 搭建
    SSH隧道 访问内网机
    笔记《鸟哥的Linux私房菜》7 Linux档案与目录管理
    Tornado 错误 "Global name 'memoryview' is not defined"
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12817079.html
Copyright © 2011-2022 走看看