forked from ex01tus/leetcode-grind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWallsAndGates.java
More file actions
44 lines (37 loc) · 1.31 KB
/
WallsAndGates.java
File metadata and controls
44 lines (37 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package graph;
import java.util.LinkedList;
import java.util.Queue;
/**
* Description: https://leetcode.com/problems/walls-and-gates
* Difficulty: Medium
* Time complexity: O(m * n)
* Space complexity: O(m * n)
*/
public class WallsAndGates {
public void wallsAndGates(int[][] rooms) {
int[][] directions = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int[][] visited = new int[rooms.length][rooms[0].length];
Queue<int[]> planned = new LinkedList<>();
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[0].length; j++) {
if (rooms[i][j] == 0) {
planned.offer(new int[]{i, j});
visited[i][j] = 1;
}
}
}
while (!planned.isEmpty()) {
int[] current = planned.poll();
for (int[] dir : directions) {
int x = current[0] + dir[0];
int y = current[1] + dir[1];
if (x >= 0 && x < rooms.length && y >= 0 && y < rooms[0].length
&& rooms[x][y] > 0 && visited[x][y] == 0) {
planned.offer(new int[]{x, y});
rooms[x][y] = rooms[current[0]][current[1]] + 1;
visited[x][y] = 1;
}
}
}
}
}