zoukankan      html  css  js  c++  java
  • [LeetCode] 1162. As Far from Land as Possible

    Given an n x n grid containing only values 0 and 1, where 0 represents water and 1 represents land, find a water cell such that its distance to the nearest land cell is maximized, and return the distance. If no land or water exists in the grid, return -1.

    The distance used in this problem is the Manhattan distance: the distance between two cells (x0, y0) and (x1, y1) is |x0 - x1| + |y0 - y1|.

    Example 1:

    Input: grid = [[1,0,1],[0,0,0],[1,0,1]]
    Output: 2
    Explanation: The cell (1, 1) is as far as possible from all the land with distance 2.
    

    Example 2:

    Input: grid = [[1,0,0],[0,0,0],[0,0,0]]
    Output: 4
    Explanation: The cell (2, 2) is as far as possible from all the land with distance 4.

    Constraints:

    • n == grid.length
    • n == grid[i].length
    • 1 <= n <= 100
    • grid[i][j] is 0 or 1

    地图分析。

    你现在手里有一份大小为 N x N 的 网格 grid,上面的每个 单元格 都用 0 和 1 标记好了。其中 0 代表海洋,1 代表陆地,请你找出一个海洋单元格,这个海洋单元格到离它最近的陆地单元格的距离是最大的。

    我们这里说的距离是「曼哈顿距离」( Manhattan Distance):(x0, y0) 和 (x1, y1) 这两个单元格之间的距离是 |x0 - x1| + |y0 - y1| 。

    如果网格上只有陆地或者海洋,请返回 -1。

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

    这道题的思路是类似 flood fill 类型的 BFS。矩阵中的 0 表示海洋,1 表示陆地,题目问的是找出一个海洋的坐标,使得这个海洋是距离陆地最远的。注意这个题是有可能没有海洋或者没有陆地的,如果这两者有任何一个不存在则返回 -1。

    一般矩阵里用 BFS 找的都是最近的点或者最短的距离,这道题找的是最远的点。既然找的是离陆地最远的海洋,那么我们先把所有陆地的坐标放到 queue 中。接着我们开始把点的坐标一个个从 queue 中弹出,然后检查周围的四个邻居,确保他们在矩阵范围内,同时坐标值是海洋。按照这样的遍历方式,每次遍历到一个海洋(0)的时候就把坐标值修改成距离。最后一个遍历到的海洋肯定就是离陆地最远的。最后我附上这个图示帮助理解(引用)。如图所示,那些一开始为 0 的坐标都是从 1 开始一点点往外扩散被修改成新的距离的,所以最后一个被修改的坐标就一定是离陆地最远的海洋。

    时间O(mn)

    空间O(mn) - size最大为 mn 的 queue

    Java实现

     1 class Solution {
     2     public int maxDistance(int[][] grid) {
     3         int m = grid.length;
     4         int n = grid[0].length;
     5         Queue<int[]> queue = new LinkedList<>();
     6         for (int i = 0; i < m; i++) {
     7             for (int j = 0; j < n; j++) {
     8                 if (grid[i][j] == 1) {
     9                     queue.offer(new int[] { i, j });
    10                 }
    11             }
    12         }
    13 
    14         // 找到海洋的flag
    15         boolean hasOcean = false;
    16         int[] point = null;
    17         int[][] DIRS = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
    18         while (!queue.isEmpty()) {
    19             point = queue.poll();
    20             int x = point[0];
    21             int y = point[1];
    22             for (int[] dir : DIRS) {
    23                 int r = x + dir[0];
    24                 int c = y + dir[1];
    25                 if (r < 0 || c < 0 || r >= m || c >= n || grid[r][c] != 0) {
    26                     continue;
    27                 }
    28                 grid[r][c] = grid[x][y] + 1;
    29                 hasOcean = true;
    30                 queue.offer(new int[] { r, c });
    31             }
    32         }
    33 
    34         if (point == null || hasOcean == false) {
    35             return -1;
    36         }
    37         // 最后一个遍历到的海洋就是最远的
    38         return grid[point[0]][point[1]] - 1;
    39     }
    40 }

    LeetCode 题目总结

  • 相关阅读:
    httprunner-2-linux下搭建hrun(下)
    Docker学习3-简单shell脚本安装mysql5.7与docker小技巧
    功能测试--聊天功能测试&微信聊天
    Fiddler抓包3-查看get与post请求
    面向对象--继承
    Mybatis入门
    Maven基础
    Cookie&Session
    Redis应用
    Redis概念和安装
  • 原文地址:https://www.cnblogs.com/cnoodle/p/15645558.html
Copyright © 2011-2022 走看看