Description
You are given a m x n 2D grid initialized with these three possible values.
-
-1
- A wall or an obstacle. -
0
- A gate. -
INF
- Infinity means an empty room. We use the value2<sup>31</sup> - 1 = 2147483647
to representINF
as you may assume that the distance to a gate is less than2147483647
.
Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF
.
For example, given the 2D grid:
After running your function, the 2D grid should be:
Solution
DFS, time O(k * m * n), space O(1)
从所有点出发太耗时了,而且会Stack Overflow,不如反过来想,从empty room(即val为0)的位置出发,做DFS填写数组。
class Solution {
public void wallsAndGates(int[][] rooms) {
if (rooms == null || rooms.length == 0 || rooms[0].length == 0) {
return;
}
for (int i = 0; i < rooms.length; ++i) {
for (int j = 0; j < rooms[0].length; ++j) {
if (rooms[i][j] == 0) {
dfsExpand(rooms, i, j, 0);
}
}
}
}
public void dfsExpand(int[][] rooms, int i, int j, int dis) {
if (i < 0 || i >= rooms.length || j < 0 || j >= rooms[0].length
|| rooms[i][j] < dis) { // rooms[i][j] is either -1 or 0
return;
}
rooms[i][j] = dis++;
dfsExpand(rooms, i - 1, j, dis);
dfsExpand(rooms, i + 1, j, dis);
dfsExpand(rooms, i, j - 1, dis);
dfsExpand(rooms, i, j + 1, dis);
}
}
BFS, time O(m * n), space O(m * n)
乍一看下面这个solution是有问题的,如果有多个gate,某个room的dis如何得到更新呢?但是细细想来,这个bfs算法就像是层序遍历一样,能够保证按照dis由小到大地处理节点,那么这样一来,在queue中第一次访问到某个节点时的dis,就是这个节点的最小dis。
注意在queue offer的时候便更新dis,而非等到queue poll的时候。一是因为在添加到queue的时候就确定了dis,还有这样做能保证每个位置最多进队列一次。
class Solution {
private static final int EMPTY = Integer.MAX_VALUE;
private static final int GATE = 0;
private static 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 });
}
}
}
}