-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
36 lines (30 loc) · 1.01 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
class Solution {
vector<array<int, 2>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
public:
int closedIsland(vector<vector<int>>& grid) {
int n = size(grid);
int m = size(grid[0]);
function<void(int, int)> dfs = [&](int i, int j) -> void {
if (i < 0 || i >= n || j < 0 || j >= m) return;
if (grid[i][j] != 0) return;
grid[i][j] = 2;
for (auto [di, dj] : directions) dfs(i + di, j + dj);
};
for (int i = 0; i < n; ++i) dfs(i, 0), dfs(i, m - 1);
for (int i = 0; i < m; ++i) dfs(0, i), dfs(n - 1, i);
int count = 0;
for (int i = 1; i < n - 1; ++i) {
for (int j = 1; j < m - 1; ++j) {
if (grid[i][j] != 0) continue;
dfs(i, j);
++count;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (grid[i][j] == 2) grid[i][j] = 0;
}
}
return count;
}
};