leet-code/src/main/java/leet/book/queueStack/WallsAndGates.java

57 lines
1.7 KiB
Java
Raw Normal View History

2021-07-02 10:47:18 +08:00
package leet.book.queueStack;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* Created with IntelliJ IDEA.
*
* @author: 轩辕龙儿
* @date: 2021/7/2 10:46
* @Description: No Description
*/
public class WallsAndGates {
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});
}
}
}
}
}