[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         boolean land = false;
 6         boolean water = false;
 7         Queue<int[]> queue = new LinkedList<>();
 8         for (int i = 0; i < m; i++) {
 9             for (int j = 0; j < n; j++) {
10                 // 只将陆地的坐标放入队列
11                 if (grid[i][j] == 1) {
12                     land = true;
13                     queue.offer(new int[] { i, j });
14                 } else if (grid[i][j] == 0) {
15                     water = true;
16                 }
17             }
18         }
19         // 没有找到陆地或者没有找到海洋就返回-1
20         if (!land || !water) {
21             return -1;
22         }
23         
24         int res = 0;
25         int[] cur = null;
26         int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
27         while (!queue.isEmpty()) {
28             cur = queue.poll();
29             int x = cur[0];
30             int y = cur[1];
31             for (int[] dir : dirs) {
32                 int newX = x + dir[0];
33                 int newY = y + dir[1];
34                 if (newX < 0 || newX >= m || newY < 0 || newY >= n || grid[newX][newY] != 0) {
35                     continue;
36                 }
37                 grid[newX][newY] = grid[x][y] + 1;
38                 queue.offer(new int[] { newX, newY });
39             }
40         }
41         return grid[cur[0]][cur[1]] - 1;
42     }
43 }

 

LeetCode 题目总结

posted @ 2021-12-05 14:36  CNoodle  阅读(135)  评论(0编辑  收藏  举报