|
11 | 11 |
|
12 | 12 | ## [1162. As Far from Land as Possible (Medium)](https://leetcode.com/problems/as-far-from-land-as-possible "地图分析")
|
13 | 13 |
|
14 |
| -<p>Given an N x N <code>grid</code> containing only values <code>0</code> and <code>1</code>, where <code>0</code> represents water and <code>1</code> represents land, find a water cell such that its distance to the nearest land cell is maximized and return the distance.</p> |
| 14 | +<p>Given an <code>n x n</code> <code>grid</code> containing only values <code>0</code> and <code>1</code>, where <code>0</code> represents water and <code>1</code> 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 <code>-1</code>.</p> |
15 | 15 |
|
16 |
| -<p>The distance used in this problem is the <em>Manhattan distance</em>: the distance between two cells <code>(x0, y0)</code> and <code>(x1, y1)</code> is <code>|x0 - x1| + |y0 - y1|</code>.</p> |
17 |
| - |
18 |
| -<p>If no land or water exists in the grid, return <code>-1</code>.</p> |
| 16 | +<p>The distance used in this problem is the Manhattan distance: the distance between two cells <code>(x0, y0)</code> and <code>(x1, y1)</code> is <code>|x0 - x1| + |y0 - y1|</code>.</p> |
19 | 17 |
|
20 | 18 | <p> </p>
|
21 |
| - |
22 | 19 | <p><strong>Example 1:</strong></p>
|
23 |
| - |
24 |
| -<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/05/03/1336_ex1.JPG" style="width: 185px; height: 87px;" /></strong></p> |
25 |
| - |
| 20 | +<img alt="" src="https://assets.leetcode.com/uploads/2019/05/03/1336_ex1.JPG" style="width: 185px; height: 87px;" /> |
26 | 21 | <pre>
|
27 |
| -<strong>Input: </strong><span id="example-input-1-1">[[1,0,1],[0,0,0],[1,0,1]]</span> |
28 |
| -<strong>Output: </strong><span id="example-output-1">2</span> |
29 |
| -<strong>Explanation: </strong> |
30 |
| -The cell (1, 1) is as far as possible from all the land with distance 2. |
| 22 | +<strong>Input:</strong> grid = [[1,0,1],[0,0,0],[1,0,1]] |
| 23 | +<strong>Output:</strong> 2 |
| 24 | +<strong>Explanation:</strong> The cell (1, 1) is as far as possible from all the land with distance 2. |
31 | 25 | </pre>
|
32 | 26 |
|
33 | 27 | <p><strong>Example 2:</strong></p>
|
34 |
| - |
35 |
| -<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/05/03/1336_ex2.JPG" style="width: 184px; height: 87px;" /></strong></p> |
36 |
| - |
| 28 | +<img alt="" src="https://assets.leetcode.com/uploads/2019/05/03/1336_ex2.JPG" style="width: 184px; height: 87px;" /> |
37 | 29 | <pre>
|
38 |
| -<strong>Input: </strong><span id="example-input-2-1">[[1,0,0],[0,0,0],[0,0,0]]</span> |
39 |
| -<strong>Output: </strong><span id="example-output-2">4</span> |
40 |
| -<strong>Explanation: </strong> |
41 |
| -The cell (2, 2) is as far as possible from all the land with distance 4. |
| 30 | +<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,0]] |
| 31 | +<strong>Output:</strong> 4 |
| 32 | +<strong>Explanation:</strong> The cell (2, 2) is as far as possible from all the land with distance 4. |
42 | 33 | </pre>
|
43 | 34 |
|
44 | 35 | <p> </p>
|
45 |
| - |
46 |
| -<p><span><strong>Note:</strong></span></p> |
47 |
| - |
48 |
| -<ol> |
49 |
| - <li><span><code>1 <= grid.length == grid[0].length <= 100</code></span></li> |
50 |
| - <li><span><code>grid[i][j]</code> is <code>0</code> or <code>1</code></span></li> |
51 |
| -</ol> |
| 36 | +<p><strong>Constraints:</strong></p> |
| 37 | + |
| 38 | +<ul> |
| 39 | + <li><code>n == grid.length</code></li> |
| 40 | + <li><code>n == grid[i].length</code></li> |
| 41 | + <li><code>1 <= n <= 100</code></li> |
| 42 | + <li><code>grid[i][j]</code> is <code>0</code> or <code>1</code></li> |
| 43 | +</ul> |
52 | 44 |
|
53 | 45 | ### Related Topics
|
54 | 46 | [[Breadth-first Search](../../tag/breadth-first-search/README.md)]
|
|
0 commit comments