-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathcheck-if-the-rectangle-corner-is-reachable.cpp
273 lines (255 loc) · 9.01 KB
/
check-if-the-rectangle-corner-is-reachable.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Time: O(n^2)
// Space: O(n)
// iterative dfs
class Solution {
public:
bool canReachCorner(int X, int Y, vector<vector<int>>& circles) {
const auto& check = [](int64_t x1, int64_t y1, int64_t r1, int64_t x2, int64_t y2, int64_t r2) {
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) <= (r1 + r2) * (r1 + r2);
};
const auto& iter_dfs = [&]() {
vector<char> lookup(size(circles));
vector<char> dst(size(circles));
vector<int> stk;
for (int u = 0; u < size(circles); ++u) {
const int x = circles[u][0], y = circles[u][1], r = circles[u][2];
if (x - r <= 0 || y + r >= Y) {
lookup[u] = true;
stk.emplace_back(u);
}
if (x + r >= X || y - r <= 0) {
dst[u] = true;
}
}
while (!empty(stk)) {
const int u = stk.back(); stk.pop_back();
if (dst[u]) {
return true;
}
const int x1 = circles[u][0], y1 = circles[u][1], r1 = circles[u][2];
for (int v = 0; v < size(circles); ++v) {
const int x2 = circles[v][0], y2 = circles[v][1], r2 = circles[v][2];
if (lookup[v] || !check(x1, y1, r1, x2, y2, r2)) {
continue;
}
lookup[v] = true;;
stk.emplace_back(v);
}
}
return false;
};
return !iter_dfs();
}
};
// Time: O(n^2)
// Space: O(n)
// bfs
class Solution2 {
public:
bool canReachCorner(int X, int Y, vector<vector<int>>& circles) {
const auto& check = [](int64_t x1, int64_t y1, int64_t r1, int64_t x2, int64_t y2, int64_t r2) {
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) <= (r1 + r2) * (r1 + r2);
};
const auto& bfs = [&]() {
vector<char> lookup(size(circles));
vector<char> dst(size(circles));
vector<int> q;
for (int u = 0; u < size(circles); ++u) {
const int x = circles[u][0], y = circles[u][1], r = circles[u][2];
if (x - r <= 0 || y + r >= Y) {
lookup[u] = true;
q.emplace_back(u);
}
if (x + r >= X || y - r <= 0) {
dst[u] = true;
}
}
while (!empty(q)) {
vector<int> new_q;
for (const auto& u : q) {
if (dst[u]) {
return true;
}
const int x1 = circles[u][0], y1 = circles[u][1], r1 = circles[u][2];
for (int v = 0; v < size(circles); ++v) {
const int x2 = circles[v][0], y2 = circles[v][1], r2 = circles[v][2];
if (lookup[v] || !check(x1, y1, r1, x2, y2, r2)) {
continue;
}
lookup[v] = true;;
new_q.emplace_back(v);
}
}
q = move(new_q);
}
return false;
};
return !bfs();
}
};
// Time: O(n^2)
// Space: O(n^2)
// iterative dfs
class Solution3 {
public:
bool canReachCorner(int X, int Y, vector<vector<int>>& circles) {
const auto& check = [](int64_t x1, int64_t y1, int64_t r1, int64_t x2, int64_t y2, int64_t r2) {
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) <= (r1 + r2) * (r1 + r2);
};
vector<vector<int>> adj(size(circles) + 2);
const auto& iter_dfs = [&](int src, int dst) {
vector<char> lookup(size(adj));
lookup[src] = true;
vector<int> stk = {src};
while (!empty(stk)) {
const int u = stk.back(); stk.pop_back();
if (u == dst) {
return true;
}
for (const auto& v : adj[u]) {
if (lookup[v]) {
continue;
}
lookup[v] = true;
stk.emplace_back(v);
}
}
return false;
};
for (int u = 0; u < size(circles); ++u) {
const int x1 = circles[u][0], y1 = circles[u][1], r1 = circles[u][2];
if (x1 - r1 <= 0 || y1 + r1 >= Y) {
adj[u].emplace_back(size(circles));
adj[size(circles)].emplace_back(u);
}
if (x1 + r1 >= X || y1 - r1 <= 0) {
adj[u].emplace_back(size(circles) + 1);
adj[size(circles) + 1].emplace_back(u);
}
for (int v = 0; v < u; ++v) {
const int x2 = circles[v][0], y2 = circles[v][1], r2 = circles[v][2];
if (!check(x1, y1, r1, x2, y2, r2)) {
continue;
}
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
}
return !iter_dfs(size(circles), size(circles) + 1);
}
};
// Time: O(n^2)
// Space: O(n^2)
// bfs
class Solution4 {
public:
bool canReachCorner(int X, int Y, vector<vector<int>>& circles) {
const auto& check = [](int64_t x1, int64_t y1, int64_t r1, int64_t x2, int64_t y2, int64_t r2) {
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) <= (r1 + r2) * (r1 + r2);
};
vector<vector<int>> adj(size(circles) + 2);
const auto& bfs = [&](int src, int dst) {
vector<char> lookup(size(adj));
lookup[src] = true;
vector<int> q = {src};
while (!empty(q)) {
vector<int> new_q;
for (const auto& u : q) {
for (const auto& v : adj[u]) {
if (lookup[v]) {
continue;
}
lookup[v] = true;
new_q.emplace_back(v);
}
}
q = move(new_q);
}
return lookup[dst];
};
for (int u = 0; u < size(circles); ++u) {
const int x1 = circles[u][0], y1 = circles[u][1], r1 = circles[u][2];
if (x1 - r1 <= 0 || y1 + r1 >= Y) {
adj[u].emplace_back(size(circles));
adj[size(circles)].emplace_back(u);
}
if (x1 + r1 >= X || y1 - r1 <= 0) {
adj[u].emplace_back(size(circles) + 1);
adj[size(circles) + 1].emplace_back(u);
}
for (int v = 0; v < u; ++v) {
const int x2 = circles[v][0], y2 = circles[v][1], r2 = circles[v][2];
if (!check(x1, y1, r1, x2, y2, r2)) {
continue;
}
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
}
return !bfs(size(circles), size(circles) + 1);
}
};
// Time: O(n^2)
// Space: O(n)
// union find
class Solution_TLE {
public:
bool canReachCorner(int X, int Y, vector<vector<int>>& circles) {
const auto& check = [](int64_t x1, int64_t y1, int64_t r1, int64_t x2, int64_t y2, int64_t r2) {
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) <= (r1 + r2) * (r1 + r2);
};
vector<vector<int>> adj(size(circles) + 2);
UnionFind uf(size(circles) + 2);
for (int u = 0; u < size(circles); ++u) {
const int x1 = circles[u][0], y1 = circles[u][1], r1 = circles[u][2];
if (x1 - r1 <= 0 || y1 + r1 >= Y) {
uf.union_set(u, size(circles));
}
if (x1 + r1 >= X || y1 - r1 <= 0) {
uf.union_set(u, size(circles) + 1);
}
for (int v = 0; v < u; ++v) {
const int x2 = circles[v][0], y2 = circles[v][1], r2 = circles[v][2];
if (!check(x1, y1, r1, x2, y2, r2)) {
continue;
}
uf.union_set(u, v);
}
}
return uf.find_set(size(circles)) != uf.find_set(size(circles) + 1);
}
private:
class UnionFind {
public:
UnionFind(int n)
: set_(n)
, rank_(n) {
iota(begin(set_), end(set_), 0);
}
int find_set(int x) {
vector<int> stk;
while (set_[x] != x) { // path compression
stk.emplace_back(x);
x = set_[x];
}
return x;
}
bool union_set(int x, int y) {
x = find_set(x), y = find_set(y);
if (x == y) {
return false;
}
if (rank_[x] > rank_[y]) {
swap(x, y);
}
set_[x] = y; // Union by rank.
if (rank_[x] == rank_[y]) {
++rank_[y];
}
return true;
}
private:
vector<int> set_;
vector<int> rank_;
};
};