UVa 1029 - Heliport

contents

  1. 1. Problem
  2. 2. Sample Input
  3. 3. Sample Output
  4. 4. Solution

Problem

逆時針順序給定一個正交多邊形,請問內接最大圓為何?

Sample Input

1
2
3
4
5
4
2 R 2 U 2 L 2 D
10
10 R 10 U 10 L 10 U 10 R 5 U 30 L 20 D 20 R 5 D
0

Sample Output

1
2
3
Case Number 1 radius is: 1.00
Case Number 2 radius is: 10.00

Solution

首先,這題不能模擬退火,至少我的技巧退火沒結果。

於是二分最大圓半徑,接著窮舉所有可能匹配的圓,針對圓檢查是否包含於正交多邊形內部。

決定半徑之後,圓心存在於幾種可能

  • 通過圓上的兩點,可以決定兩個圓心。
  • 兩條線相切於圓,可以決定四個圓心。
  • 交一點、切一線,可以決定兩個圓心。

首先必須檢測圓心是否在正交多邊形中,用了射線法莫名其妙 WA,所以建議特別考慮於正交多邊形的內部判定。隨後檢查所有線段不會部分或全部在圓內部。

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
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
#define eps 1e-6
struct Pt {
double x, y;
Pt(double a = 0, double b = 0):
x(a), y(b) {}
bool operator<(const Pt &a) const {
if (fabs(x-a.x) > eps)
return x < a.x;
if (fabs(y-a.y) > eps)
return y < a.y;
return false;
}
Pt operator-(const Pt &a) const {
return Pt(x - a.x, y - a.y);
}
Pt operator+(const Pt &a) const {
return Pt(x + a.x, y + a.y);
}
void read() {
scanf("%lf %lf", &x, &y);
}
};
struct Seg {
Pt s, e;
Seg(Pt a = Pt(), Pt b = Pt()):
s(a), e(b) {}
};
double dist(Pt a, Pt b) {
return hypot(a.x - b.x, a.y - b.y);
}
double dot(Pt a, Pt b) {
return a.x * b.x + a.y * b.y;
}
double cross2(Pt a, Pt b) {
return a.x * b.y - a.y * b.x;
}
double cross(Pt o, Pt a, Pt b) {
return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
int between(Pt a, Pt b, Pt c) {
return dot(c - a, b - a) >= 0 && dot(c - b, a - b) >= 0;
}
int onSeg(Pt a, Pt b, Pt c) {
return between(a, b, c) && fabs(cross(a, b, c)) < eps;
}
int intersection(Pt as, Pt at, Pt bs, Pt bt) {
if(cross(as, at, bs) * cross(as, at, bt) < 0 &&
cross(at, as, bs) * cross(at, as, bt) < 0 &&
cross(bs, bt, as) * cross(bs, bt, at) < 0 &&
cross(bt, bs, as) * cross(bt, bs, at) < 0)
return 1;
return 0;
}
double distProjection(Pt as, Pt at, Pt s) {
int a, b, c;
a = at.y - as.y;
b = as.x - at.x;
c = - (a * as.x + b * as.y);
return fabs(a * s.x + b * s.y + c) / hypot(a, b);
}
double ptToSeg(Seg seg, Pt p) {
double c = 1e+30;
if(between(seg.s, seg.e, p))
c = min(c, distProjection(seg.s, seg.e, p));
else
c = min(c, min(dist(seg.s, p), dist(seg.e, p)));
return c;
}
int inPolygon(Pt p[], int n, Pt q) {
int i, j, cnt = 0;
for(i = 0, j = n-1; i < n; j = i++) {
if(onSeg(q, p[i], p[j]))
return 1;
if(p[i].y > q.y != p[j].y > q.y &&
q.x < (p[j].x-p[i].x)*(q.y-p[i].y)/(p[j].y-p[i].y) + p[i].x)
cnt++;
}
return cnt&1;
}
// this problem
Pt D[1024];
int N;
int checkCircle(double x, double y, double r) {
int cnt = 0;
for (int i = 0; i < N; i++) {
if (D[i].x > x && (D[i].y > y != D[i+1].y > y))
cnt++;
}
if (cnt%2 == 0) return 0;
for (int i = 0; i < N; i++) {
if ((D[i].x - x) * (D[i].x - x) + (D[i].y - y) * (D[i].y - y) < (r - eps)* (r - eps))
return 0;
if (D[i].x == D[i + 1].x && (D[i].y > y != D[i+1].y > y) && fabs(D[i].x - x) < r - eps)
return 0;
if (D[i].y == D[i + 1].y && (D[i].x > x != D[i+1].x > x) && fabs(D[i].y - y) < r - eps)
return 0;
}
return 1;
}
int checkRadius(double r) {
for (int i = 0; i < N; i++) {
if (D[i].x == D[i+1].x)
for (int j = 0; j < N; j++) {
if (D[j].y == D[j+1].y) {
if (checkCircle(D[i].x + r, D[j].y + r, r))
return 1;
if (checkCircle(D[i].x + r, D[j].y - r, r))
return 1;
if (checkCircle(D[i].x - r, D[j].y + r, r))
return 1;
if (checkCircle(D[i].x - r, D[j].y - r, r))
return 1;
}
}
}
double d, dd, dx, dy, x, y;
for (int i = 0; i < N; i++) { // (line, point) => circle
for (int j = 0; j < N; j++) {
if (D[i].x == D[i+1].x) {
d = fabs(D[j].x - (D[i].x + r));
if (d < r) {
dy = sqrt(r*r - d*d);
if (checkCircle(D[i].x + r, D[j].y + dy, r))
return 1;
if (checkCircle(D[i].x + r, D[j].y - dy, r))
return 1;
}
d = fabs(D[j].x - (D[i].x - r));
if (d < r) {
dy = sqrt(r*r - d*d);
if (checkCircle(D[i].x - r, D[j].y + dy, r))
return 1;
if (checkCircle(D[i].x - r, D[j].y - dy, r))
return 1;
}
} else {
d = fabs(D[j].y - (D[i].y + r));
if (d < r) {
dx = sqrt(r*r - d*d);
if (checkCircle(D[j].x + dx, D[i].y + r, r))
return 1;
if (checkCircle(D[j].x - dx, D[i].y + r, r))
return 1;
}
d = fabs(D[j].y - (D[i].y - r));
if (d < r) {
dx = sqrt(r*r - d*d);
if (checkCircle(D[j].x + dx, D[i].y - r, r))
return 1;
if (checkCircle(D[j].x - dx, D[i].y - r, r))
return 1;
}
}
}
}
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
x = (D[i].x + D[j].x)/2;
y = (D[i].y + D[j].y)/2;
d = hypot(x - D[i].x, y - D[i].y);
if (d < r) {
dd = sqrt(r*r - d*d);
dx = (y - D[i].y)/d * dd;
dy = (D[i].x - x)/d * dd;
if (checkCircle(x + dx, y + dy, r))
return 1;
if (checkCircle(x - dx, y - dy, r))
return 1;
}
}
}
return 0;
}
int main() {
int n, x, cases = 0;
char cmd[1024];
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int R[128];
R['R'] = 0, R['U'] = 1, R['L'] = 2, R['D'] = 3;
while(scanf("%d", &n) == 1 && n) {
D[0] = Pt(0, 0), N = n;
for (int i = 1; i <= n; i++) {
scanf("%d %s", &x, cmd);
D[i].x = D[i-1].x + x * dx[R[cmd[0]]];
D[i].y = D[i-1].y + x * dy[R[cmd[0]]];
}
double l = 0, r = 1000, ret, mid;
while (fabs(l - r) > eps) {
mid = (l + r)/2;
if (checkRadius(mid))
l = mid, ret = mid;
else
r = mid;
}
if (cases) puts("");
printf("Case Number %d radius is: %.2lf\n", ++cases, ret);
}
return 0;
}
/*
4
2 R 2 U 2 L 2 D
10
10 R 10 U 10 L 10 U 10 R 5 U 30 L 20 D 20 R 5 D
12
1 R 1 U 1 R 1 U 1 L 1 U
1 L 1 D 1 L 1 D 1 R 1 D
*/