forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum-number-of-days-to-disconnect-island.cpp
62 lines (58 loc) · 1.92 KB
/
minimum-number-of-days-to-disconnect-island.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
// Time: O(m^2 * n^2)
// Space: O(m * n)
class Solution {
public:
int minDays(vector<vector<int>>& grid) {
if (count_islands(grid) != 1) {
return 0;
}
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid[0].size(); ++j) {
if (grid[i][j] == 0) {
continue;
}
grid[i][j] = 0;
int islands_cnt = count_islands(grid);
grid[i][j] = 1;
if (islands_cnt != 1) {
return 1;
}
}
}
return 2;
}
private:
int count_islands(const vector<vector<int>>& grid) {
vector<vector<int>> lookup(grid.size(), vector<int>(grid[0].size()));
int islands_cnt = 0;
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid[0].size(); ++j) {
if (grid[i][j] == 0 || lookup[i][j]) {
continue;
}
++islands_cnt;
floodfill(grid, i, j, &lookup);
}
}
return islands_cnt;
}
void floodfill(const vector<vector<int>>& grid, int i, int j, vector<vector<int>> *lookup) {
static const vector<pair<int, int>> directions{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
vector<pair<int, int>> stk = {{i, j}};
(*lookup)[i][j] = 1;
while (!stk.empty()) {
auto [i, j] = stk.back(); stk.pop_back();
for (const auto& [di, dj] : directions) {
const auto& [ni, nj] = pair(i + di, j + dj);
if (!(0 <= ni && ni < grid.size() &&
0 <= nj && nj < grid[0].size() &&
grid[ni][nj] &&
!(*lookup)[ni][nj])) {
continue;
}
(*lookup)[ni][nj] = 1;
stk.emplace_back(ni, nj);
}
}
}
};