-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC1568.cpp
More file actions
executable file
·66 lines (55 loc) · 1.48 KB
/
LC1568.cpp
File metadata and controls
executable file
·66 lines (55 loc) · 1.48 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Problem Statement: https://leetcode.com/problems/minimum-number-of-days-to-disconnect-island/
Time: O((m • n)²)
Space: O(m • n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
private:
int m, n;
// traverse directions easily
vector<int> xdir = {-1, 0, 1, 0};
vector<int> ydir = {0, -1, 0, 1};
bool is_valid(int x, int y) {
return (x >= 0 && x < m && y >= 0 && y < n);
}
void flood_fill(int x, int y, vector<vector<int>>& grid, vector<vector<bool>>& visited) {
visited[x][y] = true;
for (int k = 0; k < xdir.size(); k++) {
int new_x = x + xdir[k], new_y = y + ydir[k];
if (is_valid(new_x, new_y) && grid[new_x][new_y] && !visited[new_x][new_y])
flood_fill(new_x, new_y, grid, visited);
}
};
bool is_disconnected(vector<vector<int>>& grid) {
int components = 0;
vector<vector<bool>> visited(m, vector<bool>(n));
// flood-fill algorithm
for (int x = 0; x < m; x++)
for (int y = 0; y < n; y++)
if (grid[x][y] && !visited[x][y]) {
if (++components > 1)
return true;
flood_fill(x, y, grid, visited);
}
return components == 0;
}
public:
int minDays(vector<vector<int>>& grid) {
m = grid.size();
n = grid[0].size();
if (is_disconnected(grid))
return 0;
for (int x = 0; x < m; x++)
for (int y = 0; y < n; y++) {
if (grid[x][y] == 0)
continue;
grid[x][y] = 0;
bool found = is_disconnected(grid);
grid[x][y] = 1;
if (found)
return 1;
}
return 2;
}
};