题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4142
Time Limit: 5000MS Memory Limit: 65536K
Description
The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural to ask a question: “Where is the most distant point from the sea?” The answer to this question for Honshu was found in 1996. The most distant point is located in former Usuda Town, Nagano Prefecture, whose distance from the sea is 114.86 km.
In this problem, you are asked to write a program which, given a map of an island, finds the most distant point from the sea in the island, and reports its distance from the sea. In order to simplify the problem, we only consider maps representable by convex polygons.
Input
The input consists of multiple datasets. Each dataset represents a map of an island, which is a convex polygon. The format of a dataset is as follows.
n | ||
x1 | y1 | |
⋮ | ||
xn | yn |
Every input item in a dataset is a non-negative integer. Two input items in a line are separated by a space.
n in the first line is the number of vertices of the polygon, satisfying 3 ≤ n ≤ 100. Subsequent n lines are the x- and y-coordinates of the n vertices. Line segments (xi, yi)–(xi+1, yi+1) (1 ≤ i ≤ n − 1) and the line segment (xn, yn)–(x1, y1) form the border of the polygon in counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions. All coordinate values are between 0 and 10000, inclusive.
You can assume that the polygon is simple, that is, its border never crosses or touches itself. As stated above, the given polygon is always a convex one.
The last dataset is followed by a line containing a single zero.
Output
For each dataset in the input, one line containing the distance of the most distant point from the sea should be output. An output line should not contain extra characters such as spaces. The answer should not have an error greater than 0.00001 (10−5). You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied.
Sample Input
4
0 0
10000 0
10000 10000
0 10000
3
0 0
10000 0
7000 1000
6
0 40
100 20
250 40
250 70
100 90
0 70
3
0 0
10000 10000
5000 5001
0
Sample Output
5000.000000
494.233641
34.542948
0.353553
Problem solving report:
Description: 给定一个岛屿的地图,找到岛上最远的海洋点,并输出它与海洋的距离。
Problem solving: 首先二分出最远距离mid,然后把每条边向内移动mid,用半平面交检验即可。
Accepted Code:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
const double eps = 1e-8;
typedef struct point {
double x, y;
}vect;
struct line {
vect p;
point u, v;
};
vect operator - (const point a, const point b) {
return (vect){a.x - b.x, a.y - b.y};
}
double dist(const point a, const point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int pnz(double x) {
return x < -eps ? -1 : x > eps ? 1 : 0;
}
double Cross(const vect a, const vect b) {
return a.x * b.y - a.y * b.x;
}
int Onleft(const point p, const line l) {
return pnz(Cross(l.p, p - l.u));
}
point Inter(const line a, const line b) {
double t = Cross(b.p, a.u - b.u) / Cross(a.p, b.p);
return (point){a.u.x + t * a.p.x, a.u.y + t * a.p.y};
}
void Mobile(const line l[], line Sline[], int n, double r) {
double dis, dx, dy;
for (int i = 0; i < n; i++)
Sline[i] = l[i];
for (int i = 0; i < n; i++) {
dis = dist(l[i].u, l[i].v);
dx = (l[i].v.y - l[i].u.y) / dis * r;
dy = (l[i].v.x - l[i].u.x) / dis * r;
Sline[i].u.x -= dx, Sline[i].u.y += dy;
Sline[i].v.x -= dx, Sline[i].v.y += dy;
}
}
bool Halfplane(const line Sline[], int n) {
point Spt[n];
int Q[n] = {0};
int l = 0, r = 0;
for (int i = 1; i < n; i++) {
while (l < r && Onleft(Spt[r - 1], Sline[i]) < 0)
r--;
while (l < r && Onleft(Spt[l], Sline[i]) < 0)
l++;
Q[++r] = i;
if (!pnz(Cross(Sline[i].p, Sline[Q[r - 1]].p))) {
r--;
if (Onleft(Sline[i].u, Sline[Q[r]]) > 0)
Q[r] = i;
}
else Spt[r - 1] = Inter(Sline[i], Sline[Q[r - 1]]);
}
while (l < r && Onleft(Spt[r - 1], Sline[Q[l]]) < 0)
r--;
while (l < r && Onleft(Spt[l], Sline[Q[r]]) < 0)
l++;
if (r - l < 2)
return false;
return true;
}
int main() {
int n;
point p[MAXN];
line Sl[MAXN], Sline[MAXN];
while (scanf("%d", &n), n) {
for (int i = 0; i < n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
p[n] = p[0];
for (int i = 0; i < n; i++)
Sl[i] = (line){p[i + 1] - p[i], p[i], p[i + 1]};
double l = 0, r = 10000, mid;
while (pnz(r - l) >= 0) {
mid = (r + l) / 2;
Mobile(Sl, Sline, n, mid);
if (Halfplane(Sline, n))
l = mid + eps;
else r = mid - eps;
}
printf("%.6lf\n", l);
}
return 0;
}