forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect-cycles-in-2d-grid.cpp
103 lines (95 loc) · 3.38 KB
/
detect-cycles-in-2d-grid.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Time: O(m * n * α(n)) ~= O(m * n)
// Space: O(m * n)
class Solution {
public:
bool containsCycle(vector<vector<char>>& grid) {
UnionFind union_find(grid.size() * grid[0].size());
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid[0].size(); ++j) {
if (i && j &&
grid[i][j] == grid[i - 1][j] &&
grid[i][j] == grid[i][j - 1] &&
union_find.find_set(index(grid[0].size(), i - 1, j)) ==
union_find.find_set(index(grid[0].size(), i, j - 1))) {
return true;
}
if (i && grid[i][j] == grid[i - 1][j]) {
union_find.union_set(index(grid[0].size(), i - 1, j),
index(grid[0].size(), i, j));
}
if (j && grid[i][j] == grid[i][j - 1]) {
union_find.union_set(index(grid[0].size(), i, j - 1),
index(grid[0].size(), i, j));
}
}
}
return false;
}
private:
class UnionFind {
public:
UnionFind(const int n) : set_(n), count_(n) {
iota(set_.begin(), set_.end(), 0);
}
int find_set(const int x) {
if (set_[x] != x) {
set_[x] = find_set(set_[x]); // Path compression.
}
return set_[x];
}
void union_set(const int x, const int y) {
int x_root = find_set(x), y_root = find_set(y);
if (x_root != y_root) {
set_[min(x_root, y_root)] = max(x_root, y_root);
--count_;
}
}
int size() const {
return count_;
}
private:
vector<int> set_;
int count_;
};
int index(int n, int i, int j) {
return i * n + j;
}
};
// Time: O(m * n)
// Space: O(m * n)
class Solution2 {
public:
bool containsCycle(vector<vector<char>>& grid) {
static const vector<pair<int, int>> directions{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid[i].size(); ++j) {
if (!grid[i][j]) {
continue;
}
char val = grid[i][j];
vector<pair<int, int>> q = {{i, j}};
while (!q.empty()) {
vector<pair<int, int>> new_q;
for (const auto& [r, c] : q) {
if (!grid[r][c]) {
return true;
}
grid[r][c] = 0;
for (const auto& d : directions) {
const auto nr = r + d.first;
const auto nc = c + d.second;
if (!(0 <= nr && nr < grid.size() &&
0 <= nc && nc < grid[0].size() &&
grid[nr][nc] == val)) {
continue;
}
new_q.emplace_back(nr, nc);
}
}
q = move(new_q);
}
}
}
return false;
}
};