-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path934. Shortest Bridge.java
35 lines (35 loc) · 1.35 KB
/
934. Shortest Bridge.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
class Solution {
int row, col;
public int shortestBridge(int[][] a) {
row = a.length;
col = a[0].length;
Queue<int[]> q = new LinkedList();
for (int i = 0; i < row && q.isEmpty(); i++) {
for (int j = 0; j < col && q.isEmpty(); j++) {
if (a[i][j]==1) dfs(i, j, q, a);
}
}
int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
while (q.isEmpty()==false) {
int[] cur = q.poll();
for (int[] dir : dirs) {
int i = cur[0] + dir[0], j = cur[1] + dir[1];
if (i < 0 || i == row || j < 0 || j == col || a[i][j]==-1) continue;
if (a[i][j]==1) return cur[2];
a[i][j] = -1;
q.add(new int[]{i, j, cur[2]+1});//increase distance
}
}
return -1;
}
public void dfs(int i, int j, Queue<int[]> q, int[][] a) {
if (i < 0 || i == row || j < 0 || j == col || a[i][j]!=1) return;
//a value is 1
a[i][j] = -1;
q.add(new int[]{i, j, 0});//0 distance travelled
dfs(i+1, j, q, a);
dfs(i-1, j, q, a);
dfs(i, j+1, q, a);
dfs(i, j-1, q, a);
}
}