-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
104 lines (82 loc) · 2.8 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
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
104
class Solution {
const vector<array<int, 2>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
public:
int latestDayToCross(int row, int col, vector<vector<int>>& cells) {
auto check = [&](int t) -> bool {
queue<array<int, 2>> q;
for (int j = 0; j < col; ++j) q.push({0, j});
vector<vector<bool>> vis(row, vector<bool>(col, false));
for (int i = 0; i <= t; ++i)
vis[cells[i][0] - 1][cells[i][1] - 1] = true;
while (!q.empty()) {
auto [i, j] = q.front();
q.pop();
if (i < 0 || row <= i || j < 0 || col <= j) continue;
if (vis[i][j]) continue;
vis[i][j] = true;
if (i == row - 1) return true;
for (auto [di, dj] : directions) q.push({i + di, j + dj});
}
return false;
};
int n = cells.size();
int l = 0, r = n - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (check(m)) {
l = m + 1;
} else {
r = m - 1;
}
}
return l;
}
};
class Solution {
const vector<array<int, 2>> directions = {
{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
public:
int latestDayToCross(int row, int col, vector<vector<int>>& cells) {
int n = row * col;
vector<int> rank(n + 2, 0);
vector<int> group(n + 2);
iota(group.begin(), group.end(), 0);
function<int(int)> find = [&](int k) -> int {
if (k == group[k]) return k;
return group[k] = find(group[k]);
};
function<void(int, int)> unite = [&](int a, int b) -> void {
int pa = find(a);
int pb = find(b);
if (pa == pb) return;
if (rank[pa] < rank[pb]) {
group[pa] = pb;
} else if (rank[pb] < rank[pa]) {
group[pb] = pa;
} else {
group[pb] = pa;
++rank[pa];
}
};
vector<bool> water(n + 2, false);
for (int t = 0; t < n; ++t) {
int i = cells[t][0] - 1, j = cells[t][1] - 1;
int u = i * col + j;
water[u] = true;
for (auto [di, dj] : directions) {
int ii = i + di, jj = j + dj;
if (ii < 0 || row <= ii || jj < 0 || col <= jj) continue;
int v = ii * col + jj;
if (!water[v]) continue;
unite(u, v);
}
if (j == 0) {
unite(u, n);
} else if (j == col - 1) {
unite(u, n + 1);
}
if (find(n) == find(n + 1)) return t;
}
return -1;
}
};