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 | #include <bits/stdc++.h>
#define MAXN 200
using namespace std;
typedef long double db;
const db eps = 1e-12;
const db inf = 1.0 / 0.0;
int sgn(db x) {
if (x > eps) return 1;
else if (x < -eps) return -1;
else return 0;
}
struct Point {
db x, y;
Point() {}
Point(db x, db y): x(x), y(y) {}
db len() const {
return sqrt(x * x + y * y);
}
Point operator +(const Point &p) const {
return Point(x + p.x, y + p.y);
}
Point operator -(const Point &p) const {
return Point(x - p.x, y - p.y);
}
Point operator *(db k) const {
return Point(x * k, y * k);
}
Point operator /(db k) const {
return Point(x / k, y / k);
}
db operator *(const Point &p) const {
return x * p.y - p.x * y;
}
db operator ^(const Point &p) const {
return x * p.x + y * p.y;
}
bool operator <(const Point &p) const {
return sgn(x - p.x) < 0 || (sgn(x - p.x) == 0 && sgn(y - p.y) < 0);
}
bool operator ==(const Point &p) const {
return sgn(x - p.x) == 0 && sgn(y - p.y) == 0;
}
};
struct Line {
Point s, t;
Line() {}
Line(Point s, Point t): s(s), t(t) {}
db intersectRatio(const Line &l) const {
if (sgn((t - s) * (l.t - l.s)) == 0) return inf;
db u = (l.s - s) * (l.t - l.s), v = (t - s) * (l.t - l.s);
return u / v;
}
vector<Point> operator &(const Line &l) const {
db r = intersectRatio(l);
if (r == inf) return {};
return {s + (t - s) * r};
}
Point reflect(const Point &p) const {
db u = ((p - s) ^ (t - s)), v = (t - s).len();
return (s + (t - s) * (u / v / v)) * 2 - p;
}
db len() const {
return sqrt((s.x - t.x) * (s.x - t.x) + (s.y - t.y) * (s.y - t.y));
}
};
int X;
// s, t:起点和终点
// m1, m2:镜子的端点
// a[i]:障碍物的顶点
Point s, t, m1, m2, a[3];
// poly[i]:障碍物构成的多边形的一条边
Line poly[3];
// ms, mt:起点和终点的镜像
// ma[i]:障碍物的顶点的镜像
Point ms, mt, ma[3];
// mr:镜子
// mpoly[i]:障碍物构成的多边形的一条边的镜像
Line mr, mpoly[3];
// 求线段 l1 和 l2 是否相交
// strict 表示是否严格相交(strict == true 则在端点相交不算相交)
bool segmentIntersect(Line &l1, Line &l2, bool strict) {
db r1 = l1.intersectRatio(l2), r2 = l2.intersectRatio(l1);
if (strict) return sgn(r1) > 0 && sgn(r1 - 1) < 0 && sgn(r2) > 0 && sgn(r2 - 1) < 0;
else return sgn(r1) >= 0 && sgn(r1 - 1) <= 0 && sgn(r2) >= 0 && sgn(r2 - 1) <= 0;
}
// 检查线段 l 是否与障碍物或镜子严格相交
bool check(Line &l) {
for (int i = 0; i < 3; i++) if (segmentIntersect(l, poly[i], true)) return false;
if (segmentIntersect(l, mr, true)) return false;
return true;
}
int n;
vector<int> e[MAXN], msk[MAXN];
vector<db> v[MAXN];
db dis[MAXN];
db dijkstra(int MSK) {
for (int i = 0; i < n; i++) dis[i] = inf;
typedef pair<db, int> pdi;
priority_queue<pdi> pq;
pq.push(pdi(0, 0));
while (!pq.empty()) {
pdi p = pq.top(); pq.pop();
int sn = p.second;
if (dis[sn] < inf) continue;
dis[sn] = -p.first;
for (int i = 0; i < e[sn].size(); i++) {
int fn = e[sn][i];
if ((MSK & msk[sn][i]) == MSK)
pq.push(pdi(-dis[sn] - v[sn][i], fn));
}
}
return dis[1];
}
// 特殊情况 2 的构图
// 只要考虑起点终点,障碍物顶点和镜子端点之间的最短路即可,无需考虑可见性
void build1() {
vector<Point> keys;
keys.push_back(s); keys.push_back(t);
keys.push_back(m1); keys.push_back(m2);
for (int i = 0; i < 3; i++) keys.push_back(a[i]);
n = keys.size();
for (int i = 0; i < n; i++) e[i].clear(), v[i].clear(), msk[i].clear();
for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) {
Line l(keys[i], keys[j]);
if (!check(l)) continue;
e[i].push_back(j); v[i].push_back(l.len()); msk[i].push_back(0);
e[j].push_back(i); v[j].push_back(l.len()); msk[j].push_back(0);
}
}
// 点 p 是否能直接看到,或通过镜子看到点 goal
bool visible(Point &p, Point &goal) {
// 直接看到
Line l = Line(p, goal);
if (check(l)) return true;
// 通过镜子看到
l = Line(p, mr.reflect(goal));
// 如果要通过镜子看到,那么 p 与 goal 镜像的连线不能与障碍物,或障碍物的镜像相交
if (!segmentIntersect(l, mr, false)) return false;
for (int i = 0; i < 3; i++) {
if (segmentIntersect(l, poly[i], true)) return false;
if (segmentIntersect(l, mpoly[i], true)) return false;
}
return true;
}
// 线段 l 上的每个点是否都能看见点 goal
// border:导致可见性发生改变的直线
int visible(Line &l, Point &goal, vector<Line> &border) {
typedef pair<db, Point> pdp;
vector<pdp> vec;
vec.push_back(pdp(0, l.s));
vec.push_back(pdp(1, l.t));
// 求线段 l 与 border 的交点
for (Line &b : border) {
db r = l.intersectRatio(b);
if (sgn(r) > 0 && sgn(r - 1) < 0) vec.push_back(pdp(r, l.s + (l.t - l.s) * r));
}
sort(vec.begin(), vec.end());
// 检查经过的每个区域的可见性
// 由于每个区域都是凸的,因此只要看相邻交点的中点的可见性即可
for (int i = 1; i < vec.size(); i++) {
Point p = (vec[i - 1].second + vec[i].second) / 2;
if (!visible(p, goal)) return 0;
}
return 1;
}
// 一般情况的构图
// 考虑导致可见性改变的直线的交点,以它们为节点构图
void build2() {
// 求出可能改变可见性的直线
vector<Line> border;
border.push_back(Line(s, m1)); border.push_back(Line(s, m2));
border.push_back(Line(t, m1)); border.push_back(Line(t, m2));
for (int i = 0; i < 3; i++) {
border.push_back(Line(s, a[i]));
border.push_back(Line(t, a[i]));
}
border.push_back(Line(ms, m1)); border.push_back(Line(ms, m2));
border.push_back(Line(mt, m1)); border.push_back(Line(mt, m2));
for (int i = 0; i < 3; i++) {
border.push_back(Line(ms, a[i])); border.push_back(Line(ms, ma[i]));
border.push_back(Line(mt, a[i])); border.push_back(Line(mt, ma[i]));
}
// 计算 border 的交点,只留下镜子右边的点
vector<Point> keys;
keys.push_back(s); keys.push_back(t);
for (Line &l1 : border) for (Line &l2 : border) for (Point &p : l1 &l2)
if (sgn((m2 - m1) * (p - m1)) < 0 && find(keys.begin(), keys.end(), p) == keys.end())
keys.push_back(p);
n = keys.size();
for (int i = 0; i < n; i++) e[i].clear(), v[i].clear(), msk[i].clear();
// 判断顶点之间连线的可行性以及可见性
for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) {
Line l = Line(keys[i], keys[j]);
// 连线与障碍物相撞,则无法直接从 keys[i] 走到 keys[j]
if (!check(l)) continue;
// 求连线上每个点是否能看到起点以及终点
int tmp = visible(l, s, border) + visible(l, t, border) * 2;
e[i].push_back(j); v[i].push_back(l.len()); msk[i].push_back(tmp);
e[j].push_back(i); v[j].push_back(l.len()); msk[j].push_back(tmp);
}
}
void solve() {
scanf("%d", &X);
scanf("%Lf%Lf%Lf%Lf", &s.x, &s.y, &t.x, &t.y);
scanf("%Lf%Lf%Lf%Lf", &m1.x, &m1.y, &m2.x, &m2.y);
for (int i = 0; i < 3; i++) scanf("%Lf%Lf", &a[i].x, &a[i].y);
mr = Line(m1, m2);
ms = mr.reflect(s); mt = mr.reflect(t);
for (int i = 0; i < 3; i++) ma[i] = mr.reflect(a[i]);
for (int i = 0; i < 3; i++) {
poly[i] = Line(a[i], a[(i + 1) % 3]);
mpoly[i] = Line(ma[i], ma[(i + 1) % 3]);
}
Line st(s, t);
if (check(st)) {
// 特殊情况 1:s 和 t 的连线没有障碍
printf("%.12Lf\n", st.len() * (2 * X - 1));
} else if (X == 1) {
// 特殊情况 2:只有一个石头
build1();
db ans = dijkstra(0);
if (ans < inf) printf("%.12Lf\n", ans);
else printf("-1\n");
} else if (sgn((m2 - m1) * (s - m1)) < 0 && sgn((m2 - m1) * (t - m1)) < 0) {
// 一般情况:石头多于一个,s 和 t 的连线有障碍,起点终点都在镜子右边
build2();
db ans = dijkstra(1) + dijkstra(3) * (2 * X - 3) + dijkstra(2);
if (ans < inf) printf("%.12Lf\n", ans);
else printf("-1\n");
} else {
// 特殊情况 3:石头多于一个,s 和 t 的连线有障碍,起点终点至少一个在镜子左边
printf("-1\n");
}
}
int main() {
int tcase; scanf("%d", &tcase);
while (tcase--) solve();
return 0;
}
|