-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path934.cpp
More file actions
49 lines (48 loc) · 1.68 KB
/
934.cpp
File metadata and controls
49 lines (48 loc) · 1.68 KB
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
class Solution {
public:
vector<pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
void collectGrid(int i, int j, queue<pair<int, int>>& q, vector<vector<int>>& grid) {
if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size()) return;
if (grid[i][j] != 1) return;
grid[i][j] = 2;
q.push({i, j});
for (auto& direction : directions) {
int x = i + direction.first;
int y = j + direction.second;
collectGrid(x, y, q, grid);
}
}
int shortestBridge(vector<vector<int>>& grid) {
queue<pair<int, int>> q;
bool flag = false;
int m = grid.size();
int n = grid[0].size();
for (int i = 0; i < m && !flag; ++i) {
for (int j = 0; j < n && !flag; ++j) {
if (grid[i][j] == 1) {
collectGrid(i, j, q, grid);
flag = true;
}
}
}
int steps = 0;
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; ++i) {
auto [x, y] = q.front();
q.pop();
for (auto& direction : directions) {
int nextX = x + direction.first;
int nextY = y + direction.second;
if (nextX < 0 || nextX >= m || nextY < 0 || nextY >= n) continue;
if (grid[nextX][nextY] == 2) continue;
if (grid[nextX][nextY] == 1) return steps;
q.push({nextX, nextY});
grid[nextX][nextY] = 2;
}
}
steps++;
}
return steps;
}
};