Skip to content

Commit c3b432c

Browse files
committed
210523
1 parent 8150bc3 commit c3b432c

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

src/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import leetcode.editor.en.Q651.FourKeysKeyboard;
2323
import leetcode.editor.en.Q785.IsGraphBipartite;
2424
import leetcode.editor.en.Q91.DecodeWays;
25+
import leetcode.editor.en.Q934.ShortestBridge;
2526
import org.json.JSONArray;
2627
import org.json.JSONTokener;
2728

@@ -36,7 +37,7 @@
3637
public class Main {
3738
public static void main(String[] args) throws IOException {
3839

39-
System.out.println(new Contest().modifiedGraphEdges(5, toIntMatrix("[[4,1,-1],[2,0,-1],[0,3,-1],[4,3,-1]]"), 0, 1, 5));
40+
System.out.println(new ShortestBridge().shortestBridge(toIntMatrix("[[0,1],[1,0]]")));
4041

4142
}
4243

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package leetcode.editor.en.Q934;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
class Solution {
9+
public int shortestBridge(int[][] grid) {
10+
boolean marked = false;
11+
for (int i = 0; i < grid.length && !marked; i++) {
12+
for (int j = 0; j < grid[i].length && !marked; j++) {
13+
if (grid[i][j] == 1) {
14+
dfs(i, j, grid);
15+
marked = true;
16+
}
17+
}
18+
}
19+
Queue<int[]> q = new LinkedList<>();
20+
for (int i = 0; i < grid.length; i++) {
21+
for (int j = 0; j < grid[i].length; j++) {
22+
if (grid[i][j] == -1) {
23+
q.add(new int[]{i, j, 0});
24+
}
25+
}
26+
}
27+
28+
int ans = Integer.MAX_VALUE;
29+
while (!q.isEmpty()) {
30+
int size = q.size();
31+
for (int i = 0; i < size; i++) {
32+
int[] info = q.poll();
33+
int row = info[0];
34+
int col = info[1];
35+
int cost = info[2];
36+
37+
if (grid[row][col] == 1) {
38+
ans = Math.min(ans, cost);
39+
continue;
40+
}
41+
42+
if (isValidIdx(row + 1, col, grid) && grid[row + 1][col] != -1) {
43+
if (grid[row + 1][col] == 1) {
44+
ans = Math.min(ans, cost);
45+
} else {
46+
grid[row + 1][col] = -1;
47+
q.add(new int[]{row + 1, col, cost + 1});
48+
}
49+
}
50+
if (isValidIdx(row - 1, col, grid) && grid[row - 1][col] != -1) {
51+
if (grid[row - 1][col] == 1) {
52+
ans = Math.min(ans, cost);
53+
} else {
54+
grid[row - 1][col] = -1;
55+
q.add(new int[]{row - 1, col, cost + 1});
56+
}
57+
}
58+
59+
if (isValidIdx(row, col + 1, grid) && grid[row][col + 1] != -1) {
60+
if (grid[row][col + 1] == 1) {
61+
ans = Math.min(ans, cost);
62+
} else {
63+
grid[row][col + 1] = -1;
64+
q.add(new int[]{row, col + 1, cost + 1});
65+
}
66+
}
67+
68+
if (isValidIdx(row, col - 1, grid) && grid[row][col - 1] != -1) {
69+
if (grid[row][col - 1] == 1) {
70+
ans = Math.min(ans, cost);
71+
} else {
72+
grid[row][col - 1] = -1;
73+
q.add(new int[]{row, col - 1, cost + 1});
74+
}
75+
}
76+
}
77+
}
78+
return ans;
79+
80+
}
81+
82+
private void dfs(int row, int col, int[][] grid) {
83+
grid[row][col] = -1;
84+
if (isValidIdx(row + 1, col, grid) && grid[row + 1][col] == 1) {
85+
dfs(row + 1, col, grid);
86+
}
87+
if (isValidIdx(row - 1, col, grid) && grid[row - 1][col] == 1) {
88+
dfs(row - 1, col, grid);
89+
}
90+
91+
if (isValidIdx(row, col + 1, grid) && grid[row][col + 1] == 1) {
92+
dfs(row, col + 1, grid);
93+
}
94+
95+
if (isValidIdx(row, col - 1, grid) && grid[row][col - 1] == 1) {
96+
dfs(row, col - 1, grid);
97+
}
98+
}
99+
100+
private boolean isValidIdx(int i, int j, int[][] grid) {
101+
return i >= 0 && i < grid.length && j >= 0 && j < grid[i].length;
102+
}
103+
}
104+
//leetcode submit region end(Prohibit modification and deletion)
105+
106+
107+
public class ShortestBridge extends Solution {
108+
}

0 commit comments

Comments
 (0)