-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathShortestBridge934.java
97 lines (94 loc) · 3.04 KB
/
ShortestBridge934.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* In a given 2D binary array A, there are two islands. (An island is a 4-directionally
* connected group of 1s not connected to any other 1s.)
*
* Now, we may change 0s to 1s so as to connect the two islands together to form 1 island.
*
* Return the smallest number of 0s that must be flipped. (It is guaranteed that the answer is at least 1.)
*
* Example 1:
* Input: [[0,1],[1,0]]
* Output: 1
*
* Example 2:
* Input: [[0,1,0],[0,0,0],[0,0,1]]
* Output: 2
*
* Example 3:
* Input: [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
* Output: 1
*
* Note:
* 1 <= A.length = A[0].length <= 100
* A[i][j] == 0 or A[i][j] == 1
*/
public class ShortestBridge934 {
private int[][] DIR = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
public int shortestBridge(int[][] A) {
int m = A.length;
int n = A[0].length;
boolean[][] visited = new boolean[m][n];
Queue<int[]> edge = findOneIslandEdge(A, visited);
int res = 0;
while (!edge.isEmpty()) {
int len = edge.size();
for (int i=0; i<len; i++) {
int[] curr = edge.remove();
for (int k=0; k<4; k++) {
int ii = curr[0] + DIR[k][0];
int jj = curr[1] + DIR[k][1];
if (ii >= 0 && ii < m && jj >= 0 && jj < n && !visited[ii][jj]) {
if (A[ii][jj] == 0) {
edge.add(new int[]{ii, jj});
visited[ii][jj] = true;
} else {
return res;
}
}
}
}
res++;
}
return res;
}
private Queue<int[]> findOneIslandEdge(int[][] A, boolean[][] visited) {
int m = A.length;
int n = A[0].length;
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
if (A[i][j] == 1) {
return collectEdge(A, i, j, visited);
}
}
}
return null;
}
private Queue<int[]> collectEdge(int[][] A, int si, int sj, boolean[][] visited) {
Queue<int[]> edge = new LinkedList<>();
Queue<int[]> q = new LinkedList<>();
int m = A.length;
int n = A[0].length;
visited[si][sj] = true;
q.add(new int[]{si, sj});
while (!q.isEmpty()) {
int[] curr = q.remove();
boolean isEdge = false;
for (int k=0; k<4; k++) {
int ii = curr[0] + DIR[k][0];
int jj = curr[1] + DIR[k][1];
if (ii >= 0 && ii < m && jj >= 0 && jj < n && !visited[ii][jj]) {
if (A[ii][jj] == 0) {
isEdge = true;
} else {
q.add(new int[]{ii, jj});
visited[ii][jj] = true;
}
}
}
if (isEdge) {
edge.add(curr);
}
}
return edge;
}
}