286:墙与门

This commit is contained in:
huangge1199 2021-07-02 10:45:35 +08:00
parent 5885633735
commit 609c8feed0
3 changed files with 166 additions and 1 deletions

View File

@ -0,0 +1,114 @@
//你被给定一个 m × n 的二维网格 rooms 网格中有以下三种可能的初始化值
//
//
// -1 表示墙或是障碍物
// 0 表示一扇门
// INF 无限表示一个空的房间然后我们用 231 - 1 = 2147483647 代表 INF你可以认为通往门的距离总是小于 2147483647
//
//
//
// 你要给每个空房间位上填上该房间到 最近门的距离 如果无法到达门则填 INF 即可
//
//
//
// 示例 1
//
//
//输入rooms = [[2147483647,-1,0,2147483647],[2147483647,2147483647,2147483647,-1]
//,[2147483647,-1,2147483647,-1],[0,-1,2147483647,2147483647]]
//输出[[3,-1,0,1],[2,2,1,-1],[1,-1,2,-1],[0,-1,3,4]]
//
//
// 示例 2
//
//
//输入rooms = [[-1]]
//输出[[-1]]
//
//
// 示例 3
//
//
//输入rooms = [[2147483647]]
//输出[[2147483647]]
//
//
// 示例 4
//
//
//输入rooms = [[0]]
//输出[[0]]
//
//
//
//
// 提示
//
//
// m == rooms.length
// n == rooms[i].length
// 1 <= m, n <= 250
// rooms[i][j] -10 231 - 1
//
// Related Topics 广度优先搜索 数组 矩阵
// 👍 154 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
//286:墙与门
public class WallsAndGates {
public static void main(String[] args) {
//测试代码
Solution solution = new WallsAndGates().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
private static final int EMPTY = Integer.MAX_VALUE;
private static final int GATE = 0;
private final List<int[]> DIRECTIONS = Arrays.asList(
new int[]{1, 0},
new int[]{-1, 0},
new int[]{0, 1},
new int[]{0, -1}
);
public void wallsAndGates(int[][] rooms) {
int m = rooms.length;
if (m == 0) {
return;
}
int n = rooms[0].length;
Queue<int[]> q = new LinkedList<>();
for (int row = 0; row < m; row++) {
for (int col = 0; col < n; col++) {
if (rooms[row][col] == GATE) {
q.add(new int[]{row, col});
}
}
}
while (!q.isEmpty()) {
int[] point = q.poll();
int row = point[0];
int col = point[1];
for (int[] direction : DIRECTIONS) {
int r = row + direction[0];
int c = col + direction[1];
if (r < 0 || c < 0 || r >= m || c >= n || rooms[r][c] != EMPTY) {
continue;
}
rooms[r][c] = rooms[row][col] + 1;
q.add(new int[]{r, c});
}
}
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,51 @@
<p>你被给定一个 <code>m × n</code> 的二维网格 <code>rooms</code> ,网格中有以下三种可能的初始化值:</p>
<ol>
<li><code>-1</code> 表示墙或是障碍物</li>
<li><code>0</code> 表示一扇门</li>
<li><code>INF</code> 无限表示一个空的房间。然后,我们用 <code>2<sup>31</sup> - 1 = 2147483647</code> 代表 <code>INF</code>。你可以认为通往门的距离总是小于 <code>2147483647</code> 的。</li>
</ol>
<p>你要给每个空房间位上填上该房间到 <strong>最近门的距离</strong> ,如果无法到达门,则填 <code>INF</code> 即可。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/03/grid.jpg" style="width: 500px; height: 223px;" />
<pre>
<strong>输入:</strong>rooms = [[2147483647,-1,0,2147483647],[2147483647,2147483647,2147483647,-1],[2147483647,-1,2147483647,-1],[0,-1,2147483647,2147483647]]
<strong>输出:</strong>[[3,-1,0,1],[2,2,1,-1],[1,-1,2,-1],[0,-1,3,4]]
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>rooms = [[-1]]
<strong>输出:</strong>[[-1]]
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>rooms = [[2147483647]]
<strong>输出:</strong>[[2147483647]]
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>rooms = [[0]]
<strong>输出:</strong>[[0]]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>m == rooms.length</code></li>
<li><code>n == rooms[i].length</code></li>
<li><code>1 <= m, n <= 250</code></li>
<li><code>rooms[i][j]</code><code>-1</code><code>0</code><code>2<sup>31</sup> - 1</code></li>
</ul>
<div><div>Related Topics</div><div><li>广度优先搜索</li><li>数组</li><li>矩阵</li></div></div>\n<div><li>👍 154</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long