-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
62 lines (52 loc) · 1.68 KB
/
main.cpp
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
class Solution {
const vector<array<int, 2>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
public:
int shortestBridge(vector<vector<int>>& grid) {
int n = grid.size();
int m = grid[0].size();
struct Entry {
int i, j;
int d;
};
queue<Entry> q;
vector<vector<bool>> vis(n, vector<bool>(m, false));
function<void(int, int)> dfs = [&](int i, int j) -> void {
if (vis[i][j]) return;
vis[i][j] = true;
for (auto [di, dj] : directions) {
int ii = i + di, jj = j + dj;
if (ii < 0 || ii >= n || jj < 0 || jj >= m) continue;
if (grid[ii][jj] == 0) {
q.push({ii, jj, 1});
continue;
}
dfs(ii, jj);
}
};
bool found_land = false;
for (int i = 0; i < n; ++i) {
if (found_land) break;
for (int j = 0; j < m; ++j) {
if (found_land) break;
if (grid[i][j] == 1) {
dfs(i, j);
found_land = true;
}
}
}
while (!empty(q)) {
auto entry = q.front();
q.pop();
int i = entry.i, j = entry.j;
if (i < 0 || i >= n || j < 0 || j >= m) continue;
if (vis[i][j]) continue;
if (grid[i][j] == 1) return entry.d - 1;
vis[i][j] = true;
for (auto [di, dj] : directions) {
int ii = i + di, jj = j + dj;
q.push({i + di, j + dj, entry.d + 1});
}
}
return 0;
}
};