56
56
57
57
<!-- 这里可写通用的实现逻辑 -->
58
58
59
- DFS & BFS。
59
+ ** 方法一: DFS + BFS**
60
60
61
- 先通过 DFS 将其中一个岛屿的所有 1 置为 2,并将这个岛屿所有坐标点放入队列中;然后通过 BFS 一层层向外扩散,直至碰到另一个岛屿。
61
+ 题目求解的是最小翻转次数,使得两个岛屿相连,实际上等价于求解两个岛屿之间的最短距离。
62
+
63
+ 因此,我们可以先通过 DFS 将其中一个岛屿的所有点找出来,放到一个队列 $q$ 中。然后通过 BFS 一层层向外扩展,直至碰到另一个岛屿,此时将当前扩展的层数作为答案返回即可。
64
+
65
+ 在 DFS 和 BFS 搜索的过程中,我们直接将已经访问过的点标记为 $2$,这样就不会重复访问。
66
+
67
+ 时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 为矩阵的行数或列数。
62
68
63
69
<!-- tabs:start -->
64
70
@@ -69,39 +75,32 @@ DFS & BFS。
69
75
``` python
70
76
class Solution :
71
77
def shortestBridge (self , grid : List[List[int ]]) -> int :
72
- def find ():
73
- for i in range (m):
74
- for j in range (n):
75
- if grid[i][j] == 1 :
76
- return i, j
77
-
78
78
def dfs (i , j ):
79
79
q.append((i, j))
80
80
grid[i][j] = 2
81
81
for a, b in dirs:
82
82
x, y = i + a, j + b
83
- if 0 <= x < m and 0 <= y < n and grid[x][y] == 1 :
83
+ if 0 <= x < n and 0 <= y < n and grid[x][y] == 1 :
84
84
dfs(x, y)
85
85
86
- m, n = len (grid), len (grid[0 ])
86
+ n = len (grid)
87
+ dirs = ((0 , 1 ), (0 , - 1 ), (1 , 0 ), (- 1 , 0 ))
87
88
q = deque()
88
- dirs = [[0 , 1 ], [0 , - 1 ], [1 , 0 ], [- 1 , 0 ]]
89
- i, j = find()
89
+ i, j = next ((i, j) for i in range (n) for j in range (n) if grid[i][j])
90
90
dfs(i, j)
91
- ans = - 1
92
- while q:
93
- ans += 1
91
+ ans = 0
92
+ while 1 :
94
93
for _ in range (len (q)):
95
94
i, j = q.popleft()
96
95
for a, b in dirs:
97
96
x, y = i + a, j + b
98
- if 0 <= x < m and 0 <= y < n:
97
+ if 0 <= x < n and 0 <= y < n:
99
98
if grid[x][y] == 1 :
100
99
return ans
101
100
if grid[x][y] == 0 :
102
101
grid[x][y] = 2
103
102
q.append((x, y))
104
- return 0
103
+ ans += 1
105
104
```
106
105
107
106
### ** Java**
@@ -110,27 +109,30 @@ class Solution:
110
109
111
110
``` java
112
111
class Solution {
113
- private int [][] grid;
114
112
private int [] dirs = {- 1 , 0 , 1 , 0 , - 1 };
115
- private int m;
113
+ private Deque<int[]> q = new ArrayDeque<> ();
114
+ private int [][] grid;
116
115
private int n;
117
116
118
117
public int shortestBridge (int [][] grid ) {
119
- m = grid. length;
120
- n = grid[0 ]. length;
121
118
this . grid = grid;
122
- int [] start = find();
123
- Queue<int[]> q = new LinkedList<> ();
124
- dfs(start[0 ], start[1 ], q);
125
- int ans = - 1 ;
126
- while (! q. isEmpty()) {
127
- ++ ans;
128
- for (int k = q. size(); k > 0 ; -- k) {
129
- int [] p = q. poll();
130
- for (int i = 0 ; i < 4 ; ++ i) {
131
- int x = p[0 ] + dirs[i];
132
- int y = p[1 ] + dirs[i + 1 ];
133
- if (x >= 0 && x < m && y >= 0 && y < n) {
119
+ n = grid. length;
120
+ for (int i = 0 , x = 1 ; i < n && x == 1 ; ++ i) {
121
+ for (int j = 0 ; j < n; ++ j) {
122
+ if (grid[i][j] == 1 ) {
123
+ dfs(i, j);
124
+ x = 0 ;
125
+ break ;
126
+ }
127
+ }
128
+ }
129
+ int ans = 0 ;
130
+ while (true ) {
131
+ for (int i = q. size(); i > 0 ; -- i) {
132
+ var p = q. pollFirst();
133
+ for (int k = 0 ; k < 4 ; ++ k) {
134
+ int x = p[0 ] + dirs[k], y = p[1 ] + dirs[k + 1 ];
135
+ if (x >= 0 && x < n && y >= 0 && y < n) {
134
136
if (grid[x][y] == 1 ) {
135
137
return ans;
136
138
}
@@ -141,140 +143,124 @@ class Solution {
141
143
}
142
144
}
143
145
}
146
+ ++ ans;
144
147
}
145
- return 0 ;
146
148
}
147
149
148
- private void dfs (int i , int j , Queue<int[]> q ) {
150
+ private void dfs (int i , int j ) {
149
151
grid[i][j] = 2 ;
150
152
q. offer(new int [] {i, j});
151
153
for (int k = 0 ; k < 4 ; ++ k) {
152
- int x = i + dirs[k];
153
- int y = j + dirs[k + 1 ];
154
- if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 ) {
155
- dfs(x, y, q);
156
- }
157
- }
158
- }
159
-
160
- private int [] find () {
161
- for (int i = 0 ; i < m; ++ i) {
162
- for (int j = 0 ; j < n; ++ j) {
163
- if (grid[i][j] == 1 ) {
164
- return new int [] {i, j};
165
- }
154
+ int x = i + dirs[k], y = j + dirs[k + 1 ];
155
+ if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 1 ) {
156
+ dfs(x, y);
166
157
}
167
158
}
168
- return new int [] {0 , 0 };
169
159
}
170
160
}
171
161
```
172
162
173
163
### ** C++**
174
164
175
165
``` cpp
166
+ using pii = pair<int , int >;
167
+
176
168
class Solution {
177
169
public:
178
- vector<int > dirs = {-1, 0, 1, 0, -1};
170
+ const static inline vector<int > dirs = {-1, 0, 1, 0, -1};
179
171
180
172
int shortestBridge(vector<vector<int>>& grid) {
181
- vector<int> start = find(grid);
182
- queue<vector<int>> q;
183
- dfs(start[0], start[1], q, grid);
184
- int ans = -1;
185
- while (!q.empty()) {
186
- ++ans;
187
- for (int k = q.size(); k > 0; --k) {
188
- auto p = q.front();
173
+ int n = grid.size();
174
+ queue<pii> q;
175
+ function<void(int, int)> dfs = [&](int i, int j) {
176
+ grid[i][j] = 2;
177
+ q.emplace(i, j);
178
+ for (int k = 0; k < 4; ++k) {
179
+ int x = i + dirs[k], y = j + dirs[k + 1];
180
+ if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 1) {
181
+ dfs(x, y);
182
+ }
183
+ }
184
+ };
185
+ for (int i = 0 , x = 1 ; i < n && x; ++i) {
186
+ for (int j = 0; j < n; ++j) {
187
+ if (grid[i][j]) {
188
+ dfs (i, j);
189
+ x = 0;
190
+ break;
191
+ }
192
+ }
193
+ }
194
+ int ans = 0;
195
+ while (1) {
196
+ for (int k = q.size(); k; --k) {
197
+ auto [ i, j] = q.front();
189
198
q.pop();
190
- for (int i = 0; i < 4; ++i) {
191
- int x = p[0] + dirs[i];
192
- int y = p[1] + dirs[i + 1];
193
- if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size()) {
199
+ for (int h = 0; h < 4; ++h) {
200
+ int x = i + dirs[ h] , y = j + dirs[ h + 1] ;
201
+ if (x >= 0 && x < n && y >= 0 && y < n) {
194
202
if (grid[ x] [ y ] == 1) return ans;
195
203
if (grid[ x] [ y ] == 0) {
196
204
grid[ x] [ y ] = 2;
197
- q.push({ x, y} );
205
+ q.emplace( x, y);
198
206
}
199
207
}
200
208
}
201
209
}
202
- }
203
- return 0 ;
204
- }
205
-
206
- void dfs (int i, int j, queue<vector<int >>& q, vector<vector<int >>& grid) {
207
- grid[ i] [ j ] = 2;
208
- q.push({i, j});
209
- for (int k = 0; k < 4; ++k) {
210
- int x = i + dirs[ k] ;
211
- int y = j + dirs[ k + 1] ;
212
- if (x >= 0 && x < grid.size() && y >= 0 && y < grid[ 0] .size() && grid[ x] [ y ] == 1)
213
- dfs(x, y, q, grid);
210
+ ++ans;
214
211
}
215
212
}
216
-
217
- vector<int> find(vector<vector<int>>& grid) {
218
- for (int i = 0; i < grid.size(); ++i)
219
- for (int j = 0; j < grid[0].size(); ++j)
220
- if (grid[i][j] == 1)
221
- return {i, j};
222
- return {0, 0};
223
- }
224
213
};
225
214
```
226
215
227
216
### ** Go**
228
217
229
218
``` go
230
219
func shortestBridge (grid [][]int ) int {
231
- m, n := len(grid), len(grid[0])
232
- find := func() []int {
233
- for i := 0; i < m; i++ {
234
- for j := 0; j < n; j++ {
235
- if grid[i][j] == 1 {
236
- return []int{i, j}
237
- }
238
- }
239
- }
240
- return []int{0, 0}
241
- }
242
- start := find()
243
- var q [][]int
220
+ n := len (grid)
244
221
dirs := []int {-1 , 0 , 1 , 0 , -1 }
245
- var dfs func(i, j int)
222
+ type pair struct { i, j int }
223
+ q := []pair{}
224
+ var dfs func (int , int )
246
225
dfs = func (i, j int ) {
247
226
grid[i][j] = 2
248
- q = append(q, []int {i, j})
227
+ q = append (q, pair {i, j})
249
228
for k := 0 ; k < 4 ; k++ {
250
229
x , y := i+dirs[k], j+dirs[k+1 ]
251
- if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 {
230
+ if x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 1 {
252
231
dfs (x, y)
253
232
}
254
233
}
255
234
}
256
- dfs(start[0], start[1])
257
- ans := -1
258
- for len(q) > 0 {
259
- ans++
260
- for k := len(q); k > 0; k-- {
235
+ for i , x := 0 , 1 ; i < n && x == 1 ; i++ {
236
+ for j := 0 ; j < n; j++ {
237
+ if grid[i][j] == 1 {
238
+ dfs (i, j)
239
+ x = 0
240
+ break
241
+ }
242
+ }
243
+ }
244
+ ans := 0
245
+ for {
246
+ for i := len (q); i > 0 ; i-- {
261
247
p := q[0 ]
262
248
q = q[1 :]
263
- for i := 0; i < 4; i ++ {
264
- x, y := p[0] +dirs[i ], p[1] +dirs[i +1]
265
- if x >= 0 && x < m && y >= 0 && y < n {
249
+ for k := 0 ; k < 4 ; k ++ {
250
+ x , y := p. i +dirs[k ], p. j +dirs[k +1 ]
251
+ if x >= 0 && x < n && y >= 0 && y < n {
266
252
if grid[x][y] == 1 {
267
253
return ans
268
254
}
269
255
if grid[x][y] == 0 {
270
256
grid[x][y] = 2
271
- q = append(q, []int {x, y})
257
+ q = append (q, pair {x, y})
272
258
}
273
259
}
274
260
}
275
261
}
262
+ ans++
276
263
}
277
- return 0
278
264
}
279
265
```
280
266
0 commit comments