-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_317.java
More file actions
61 lines (55 loc) · 2.16 KB
/
Copy pathP_317.java
File metadata and controls
61 lines (55 loc) · 2.16 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
package leetcode.hard;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
public class P_317 {
private static final int[][] DIRS = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
public int shortestDistance(int[][] grid) {
final int[][] dist = new int[grid.length][grid[0].length];
final int[][] reach = new int[grid.length][grid[0].length];
int total = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
total++;
bfs(grid, dist, reach, i, j);
}
}
}
int res = Integer.MAX_VALUE;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (reach[i][j] == total) {
res = Math.min(res, dist[i][j]);
}
}
}
return res == Integer.MAX_VALUE ? -1 : res;
}
private static void bfs(int[][] grid, int[][] dist, int[][] reach, int i, int j) {
final Deque<int[]> q = new ArrayDeque<>(Collections.singleton(new int[] { i, j }));
final boolean[][] visited = new boolean[grid.length][grid[0].length];
for (int level = 0; !q.isEmpty(); level++) {
for (int k = q.size(); k > 0; k--) {
final int[] ints = q.removeFirst();
final int x = ints[0];
final int y = ints[1];
if (!visited[x][y]) {
if (grid[x][y] != 1) {
reach[x][y]++;
}
visited[x][y] = true;
dist[x][y] += level;
for (int[] dir : DIRS) {
final int nx = x + dir[0];
final int ny = y + dir[1];
if (nx >= 0 && nx < grid.length && ny >= 0 && ny < grid[0].length
&& !visited[nx][ny] && grid[nx][ny] == 0) {
q.offerLast(new int[] { nx, ny });
}
}
}
}
}
}
}