-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortestBridge.cpp
More file actions
51 lines (50 loc) · 1.68 KB
/
shortestBridge.cpp
File metadata and controls
51 lines (50 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
50
51
class Solution {
public:
int shortestBridge(vector<vector<int>>& grid) {
queue<pair<int, int> > q, q2;
auto p = getFirst(grid);
q.push(p);
q2.push(p);
grid[p.first][p.second] = 2;
int dir[5] = {1, 0, -1, 0, 1};
while(!q2.empty()) {
p = q2.front();
q2.pop();
for (int i = 0; i < 4; i++) {
int x = p.first + dir[i], y = p.second + dir[i + 1];
if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size() || grid[x][y] != 1) continue;
q2.push({x, y});
q.push({x, y});
grid[x][y] = 2;
}
}
bool flag = false;
int ans = 1;
while (!flag && !q.empty()) {
int len = q.size();
for (int j = 0; j < len && !flag; j++) {
p = q.front();
q.pop();
for (int i = 0; i < 4 && !flag; i++) {
int x = p.first + dir[i], y = p.second + dir[i + 1];
if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size() || grid[x][y] == 2) continue;
if (grid[x][y] == 1) {
flag = true;
break;
}
q.push({x, y});
grid[x][y] = 2;
}
}
if(!flag) ans++;
}
return ans - 1;
}
pair<int, int> getFirst(vector<vector<int>>& grid) {
for (int i = 0; i < grid.size(); i++)
for (int j = 0; j < grid[0].size(); j++)
if (grid[i][j] == 1)
return {i, j};
return {-1, -1};
}
};