-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWallsAndGates.java
More file actions
69 lines (60 loc) · 2.29 KB
/
Copy pathWallsAndGates.java
File metadata and controls
69 lines (60 loc) · 2.29 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.LinkedList;
import java.util.Queue;
/* Walls and Gates https://leetcode.com/problems/walls-and-gates/
* 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 value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647.
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.
Example:
Given the 2D grid:
INF -1 0 INF
INF INF INF -1
INF -1 INF -1
0 -1 INF INF
After running your function, the 2D grid should be:
3 -1 0 1
2 2 1 -1
1 -1 2 -1
0 -1 3 4 */
public class WallsAndGates {
public static void main(String[] args) {
// write your code here
int[][] input = new int[][]{{2147483647, -1, 0, 2147483647}, {2147483647, 2147483647, 2147483647, -1}, {2147483647, -1, 2147483647, -1}, {0, -1, 2147483647, 2147483647}};
wallsAndGates(input);
}
public static void wallsAndGates(int[][] rooms) {
if (rooms == null || (rooms.length == 0)) return;
Queue<int[]> gateQueue = new LinkedList<>();
int empty = 0;
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[0].length; j++) {
if (rooms[i][j] == 0) {
gateQueue.offer(new int[]{i, j});
}
if (rooms[i][j] == 2147483647) {
empty++;
}
}
}
if (empty == 0) return;
int[][] directions = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
while (!gateQueue.isEmpty()) {
int[] ele = gateQueue.poll();
for (int[] dir : directions) {
int x = ele[0] + dir[0];
int y = ele[1] + dir[1];
if (x < 0 || y < 0 || x >= rooms.length || y >= rooms[0].length || rooms[x][y] == -1)
continue;
if (rooms[x][y] == 2147483647) {
rooms[x][y] = rooms[ele[0]][ele[1]] + 1;
empty--;
gateQueue.offer(new int[]{x, y});
}
}
if (empty <= 0) {
break;
}
}
}
}