-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution_2d.cpp
356 lines (290 loc) · 9.99 KB
/
solution_2d.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <bits/stdc++.h>
#include <omp.h>
using namespace std;
typedef tuple<int, int> point;
typedef tuple<point, point> line;
typedef tuple<point, point, point> triangle;
typedef int_fast32_t fint;
fint nextInt() {
fint x;
assert(1 == scanf("%ld", &x));
return x;
}
fint N;
point s, e;
vector<triangle> obst;
vector<line> L;
vector<point> P;
map< point, vector<point> > G;
inline fint minX(point a, point b) { return min(get<0>(a), get<0>(b)); }
inline fint minY(point a, point b) { return min(get<1>(a), get<1>(b)); }
inline fint maxX(point a, point b) { return max(get<0>(a), get<0>(b)); }
inline fint maxY(point a, point b) { return max(get<1>(a), get<1>(b)); }
inline fint minX(line l){ return minX(get<0>(l), get<1>(l)); }
inline fint minY(line l){ return minY(get<0>(l), get<1>(l)); }
inline fint maxX(line l){ return maxX(get<0>(l), get<1>(l)); }
inline fint maxY(line l){ return maxY(get<0>(l), get<1>(l)); }
double distance(point a, point b) {
return sqrt(pow(1. * (get<0>(a) - get<0>(b)), 2) +
pow(1. * (get<1>(a) - get<1>(b)), 2));
}
bool onSegment(point p, point q, point r) {
if (get<0>(q) <= maxX(p, r) && get<0>(q) >= minX(p, r) &&
get<1>(q) <= maxY(p, r) && get<1>(q) >= minY(p, r))
return true;
return false;
}
fint orientation(point p, point q, point r) {
int val = (get<1>(q) - get<1>(p)) * (get<0>(r) - get<0>(q)) -
(get<0>(q) - get<0>(p)) * (get<1>(r) - get<1>(q));
if (val == 0) return 0;
return (val > 0) ? 1 : 2;
}
bool intersect(point p1, point q1, point p2, point q2) {
fint o1 = orientation(p1, q1, p2);
fint o2 = orientation(p1, q1, q2);
fint o3 = orientation(p2, q2, p1);
fint o4 = orientation(p2, q2, q1);
if (o1 != o2 && o3 != o4) return true;
if (o1 == 0 && onSegment(p1, p2, q1)) return true;
if (o2 == 0 && onSegment(p1, q2, q1)) return true;
if (o3 == 0 && onSegment(p2, p1, q2)) return true;
if (o4 == 0 && onSegment(p2, q1, q2)) return true;
return false;
}
double sign (point p1, point p2, point p3)
{
return (get<0>(p1) - get<0>(p3)) * (get<1>(p2) - get<1>(p3)) - (get<0>(p2) - get<0>(p3)) * (get<1>(p1) - get<1>(p3));
}
bool PointInTriangle (point pt, point v1, point v2, point v3)
{
bool b1 = sign(pt, v1, v2) < 0.0;
bool b2 = sign(pt, v2, v3) < 0.0;
bool b3 = sign(pt, v3, v1) < 0.0;
return ((b1 == b2) && (b2 == b3));
}
#define OFFSET 1000000
#define BUCKET 100
int main() {
fint a0 = nextInt();
fint a1 = nextInt();
fint a2 = nextInt();
fint a3 = nextInt();
s = make_tuple(a0, a1);
e = make_tuple(a2, a3);
N = nextInt();
obst.reserve(N);
for (int i = 0; i < N; i++) {
fint x1 = nextInt();
fint x2 = nextInt();
fint x3 = nextInt();
fint x4 = nextInt();
fint x5 = nextInt();
fint x6 = nextInt();
obst.push_back(
make_tuple(make_tuple(x1, x2), make_tuple(x3, x4), make_tuple(x5, x6)));
}
L.reserve(3 * N);
P.reserve(N * 24 + 2);
P.push_back(s);
P.push_back(e);
vector<line> bucketLine[2*OFFSET/BUCKET+1];
for (auto o : obst) {
auto p0 = get<0>(o);
auto p1 = get<1>(o);
auto p2 = get<2>(o);
// printf("%d %d %d %d %d %d\n", get<0>(p0), get<1>(p0), get<0>(p1),
// get<1>(p1), get<0>(p2), get<1>(p2));
auto l01 = get<0>(p0) < get<0>(p1) ? make_tuple(p0, p1) : make_tuple(p1, p0);
auto l02 = get<0>(p0) < get<0>(p2) ? make_tuple(p0, p2) : make_tuple(p2, p0);
auto l12 = get<0>(p1) < get<0>(p2) ? make_tuple(p1, p2) : make_tuple(p2, p1);
fint x0, x1;
x0 = (minX(l01)+OFFSET)/BUCKET;
x1 = (maxX(l01)+OFFSET)/BUCKET;
for(; x0 <= x1; ++x0)
bucketLine[x0].push_back(l01);
x0 = (minX(l02)+OFFSET)/BUCKET;
x1 = (maxX(l02)+OFFSET)/BUCKET;
for(; x0 <= x1; ++x0)
bucketLine[x0].push_back(l02);
x0 = (minX(l12)+OFFSET)/BUCKET;
x1 = (maxX(l12)+OFFSET)/BUCKET;
for(; x0 <= x1; ++x0)
bucketLine[x0].push_back(l12);
auto p00 = make_tuple(get<0>(p0) + 1, get<1>(p0));
auto p01 = make_tuple(get<0>(p0), get<1>(p0) + 1);
auto p02 = make_tuple(get<0>(p0) - 1, get<1>(p0));
auto p03 = make_tuple(get<0>(p0), get<1>(p0) - 1);
auto p10 = make_tuple(get<0>(p1) + 1, get<1>(p1));
auto p11 = make_tuple(get<0>(p1), get<1>(p1) + 1);
auto p12 = make_tuple(get<0>(p1) - 1, get<1>(p1));
auto p13 = make_tuple(get<0>(p1), get<1>(p1) - 1);
auto p20 = make_tuple(get<0>(p2) + 1, get<1>(p2));
auto p21 = make_tuple(get<0>(p2), get<1>(p2) + 1);
auto p22 = make_tuple(get<0>(p2) - 1, get<1>(p2));
auto p23 = make_tuple(get<0>(p2), get<1>(p2) - 1);
L.push_back(l01);
L.push_back(l02);
L.push_back(l12);
if( !PointInTriangle(p00, p0, p1, p2) ) P.push_back(p00);
if( !PointInTriangle(p01, p0, p1, p2) ) P.push_back(p01);
if( !PointInTriangle(p02, p0, p1, p2) ) P.push_back(p02);
if( !PointInTriangle(p03, p0, p1, p2) ) P.push_back(p03);
if( !PointInTriangle(p10, p0, p1, p2) ) P.push_back(p10);
if( !PointInTriangle(p11, p0, p1, p2) ) P.push_back(p11);
if( !PointInTriangle(p12, p0, p1, p2) ) P.push_back(p12);
if( !PointInTriangle(p13, p0, p1, p2) ) P.push_back(p13);
if( !PointInTriangle(p20, p0, p1, p2) ) P.push_back(p20);
if( !PointInTriangle(p21, p0, p1, p2) ) P.push_back(p21);
if( !PointInTriangle(p22, p0, p1, p2) ) P.push_back(p22);
if( !PointInTriangle(p23, p0, p1, p2) ) P.push_back(p23);
auto dp00 = make_tuple(get<0>(p0) + 1, get<1>(p0) + 1);
auto dp01 = make_tuple(get<0>(p0) + 1, get<1>(p0) - 1);
auto dp02 = make_tuple(get<0>(p0) - 1, get<1>(p0) + 1);
auto dp03 = make_tuple(get<0>(p0) - 1, get<1>(p0) - 1);
auto dp10 = make_tuple(get<0>(p1) + 1, get<1>(p1) + 1);
auto dp11 = make_tuple(get<0>(p1) + 1, get<1>(p1) - 1);
auto dp12 = make_tuple(get<0>(p1) - 1, get<1>(p1) + 1);
auto dp13 = make_tuple(get<0>(p1) - 1, get<1>(p1) - 1);
auto dp20 = make_tuple(get<0>(p2) + 1, get<1>(p2) + 1);
auto dp21 = make_tuple(get<0>(p2) + 1, get<1>(p2) - 1);
auto dp22 = make_tuple(get<0>(p2) - 1, get<1>(p2) + 1);
auto dp23 = make_tuple(get<0>(p2) - 1, get<1>(p2) - 1);
if( !PointInTriangle(dp00, p0, p1, p2) ) P.push_back(dp00);
if( !PointInTriangle(dp01, p0, p1, p2) ) P.push_back(dp01);
if( !PointInTriangle(dp02, p0, p1, p2) ) P.push_back(dp02);
if( !PointInTriangle(dp03, p0, p1, p2) ) P.push_back(dp03);
if( !PointInTriangle(dp10, p0, p1, p2) ) P.push_back(dp10);
if( !PointInTriangle(dp11, p0, p1, p2) ) P.push_back(dp11);
if( !PointInTriangle(dp12, p0, p1, p2) ) P.push_back(dp12);
if( !PointInTriangle(dp13, p0, p1, p2) ) P.push_back(dp13);
if( !PointInTriangle(dp20, p0, p1, p2) ) P.push_back(dp20);
if( !PointInTriangle(dp21, p0, p1, p2) ) P.push_back(dp21);
if( !PointInTriangle(dp22, p0, p1, p2) ) P.push_back(dp22);
if( !PointInTriangle(dp23, p0, p1, p2) ) P.push_back(dp23);
}
// printf("P prima %zu\n", P.size());
vector<point> tmp;
#pragma omp parallel for schedule(guided)
for(size_t i=0; i<P.size(); ++i)
{
auto p = P[i];
bool out = true;
for(auto o : obst)
{
if( PointInTriangle(p, get<0>(o), get<1>(o), get<2>(o)) )
{
out = false;
break;
}
}
#pragma omp critical
{
if( out ) tmp.push_back(p);
}
}
P = tmp;
printf("P dopo %zu\n", P.size());
printf("L dopo %zu\n", L.size());
fflush(stdout);
auto linecp = [](line a, line b){ return get<0>(get<0>(a)) < get<0>(get<0>(b)); };
sort(P.begin(), P.end());
sort(L.begin(), L.end(), linecp);
// printf("SORT\n");
// fflush(stdout);
/*
#pragma omp parallel for schedule(guided)
for(int i=0; i<P.size(); i++)
{
for(int j=0; j<P.size(); j++)
{
if( i == j ) continue;
bool out = true;
for (auto l : L) {
if( get<0>(get<0>(l)) > get<0>(P[i]) ) break;
if (intersect(P[i], P[j], get<0>(l), get<1>(l))) {
out = false;
break;
}
}
if( out ){
G[P[i]].push_back(P[j]);
}
}
}
int cc = 0;
for(auto g : G)
cc += g.second.size();
printf("|G| = %d\n", cc);
fflush(stdout);
*/
set<point> S(P.begin(), P.end());
map<point, double> best;
map<point, point> prec;
priority_queue<pair<double, point>> Q;
Q.push(make_pair(0., s));
best[s] = 0.;
best[e] = INT_MAX;
fint count = 0;
while (!Q.empty()) {
auto top = Q.top();
Q.pop();
if (best[top.second] != -top.first) continue;
if (S.find(top.second) == S.end()) continue;
S.erase(top.second);
printf("[%d][%d][%.6f][%ld]\n", get<0>(top.second), get<1>(top.second), top.first, count++);
fflush(stdout);
if (top.second == e) break; // END
#pragma omp parallel for schedule(guided)
for (size_t i = 0; i < P.size(); ++i) {
auto p = P[i];
if (S.find(p) == S.end()) continue;
if( abs(get<0>(p) - get<0>(top.second)) > 1000 ) continue;
if( abs(get<1>(p) - get<1>(top.second)) > 1000 ) continue;
bool inter = false;
auto pp = make_tuple(top.second, p);
if( get<0>(top.second) > get<0>(p) )
pp = make_tuple(p, top.second);
fint x0 = (minX(pp)+OFFSET)/BUCKET;
fint x1 = (maxX(pp)+OFFSET)/BUCKET;
for(; x0 <= x1; x0++)
{
for(auto l : bucketLine[x0])
if( intersect(top.second, p, get<0>(l), get<1>(l)) )
{
inter = true;
break;
}
if(inter) break;
}
if(inter) continue;
double dist = best[top.second] + distance(top.second, p);
if (best.find(p) == best.end() || dist < best[p]) {
best[p] = dist;
prec[p] = top.second;
#pragma omp critical
{
Q.push(make_pair(-dist, p));
}
}
}
}
if (prec.find(e) == prec.end()) {
printf("IMPOSSIBLE");
} else {
vector<point> output;
point last = e;
do {
output.push_back(last);
last = prec[last];
} while (last != s);
output.push_back(s);
reverse(output.begin(), output.end());
printf("%zu\n", output.size());
for (auto o : output) {
printf("%d %d\n", get<0>(o), get<1>(o));
}
}
fflush(stdout);
return 0;
}