ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LeetCode] 1162. As Far from Land as Possible

2021-12-05 15:04:20  阅读:204  来源: 互联网

标签:distance Land point Far 海洋 queue int grid LeetCode


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 题目总结

标签:distance,Land,point,Far,海洋,queue,int,grid,LeetCode
来源: https://www.cnblogs.com/cnoodle/p/15645558.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有