Most Distant Point from the Sea
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.
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 Source |
[Submit] [Go Back] [Status] [Discuss]
大括号决定不再提行了。。 要不然写只有一行的函数要死人... 发现空一格换行也挺错落有致的... 话说博客终于破两百篇了~
这道题让你求离海最远的距离, 其实就是求内切圆... 二分答案来缩小收缩各边... 然后判断有无半平面交即可. 算几的模板好长啊不过还是蛮好看的, 也还蛮好写。 只能期待指法练成有一个强大的代码能力了~ 突然想起上次东北集训有幸liu_runda学长帮我查错, 目睹了什么叫真正的手速...
Upd: 半平面交那里是first ++, 我写错了居然还能A... 醉了. (不过懒得改了
#include<stdio.h>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const double eps = 1e-8;
const int maxn = 1e5;
int n;
inline int sign(double x) {
return (x > -eps) - (x < eps);
}
struct Vector {
double x, y;
Vector(){}
Vector(double x, double y) : x(x), y(y) {}
double ang() const {
return atan2(y, x);
}
double len() const {
return sqrt(x * x + y * y);
}
inline friend bool operator < (const Vector &r, const Vector &s) {
return r.x < s.x || (r.x == s.x && r.y < s.y);
}
inline friend Vector operator + (const Vector &r, const Vector &s) {
return Vector(r.x + s.x, r.y + s.y);
}
inline friend Vector operator - (const Vector &r, const Vector &s) {
return Vector(r.x - s.x, r.y - s.y);
}
inline friend Vector operator * (const Vector &r, double s) {
return Vector(r.x * s, r.y * s);
}
inline friend Vector operator / (const Vector &r, double s) {
return Vector(r.x / s, r.y / s);
}
};
typedef Vector Point;
inline double cross(const Vector &r, const Vector &s) {
return r.x * s.y - r.y * s.x;
}
inline double dot(const Vector &r, const Vector &s) {
return r.x * s.x + r.y * s.y;
}
inline Vector normal(const Vector &u) {
return Vector(-u.y, u.x);
}
inline Vector zoom(const Vector &u, double s) {
return u * (s / u.len());
}
struct Line {
Point p;
Vector u;
double ang;
Line(){}
Line(Point p, Vector u) : p(p), u(u), ang(u.ang()) {}
inline friend bool operator < (const Line &r, const Line &s) {
return r.ang < s.ang;
}
inline friend bool operator == (const Line &r, const Line &s) {
return ! sign(r.ang - s.ang);
}
};
inline bool onleft (const Line &l, const Point &p) {
return cross(l.u, p - l.p) > 0;
}
inline bool parallel(const Line &r, const Line &s) {
return fabs(cross(r.u, s.u)) < eps;
}
inline Point Line_intersect(const Point &P, const Vector &u, const Point &Q, const Vector &v) {
double t = cross(Q - P, v) / cross(u, v);
return P + u * t;
}
inline Point Line_intersect(const Line &r, const Line &s) {
return Line_intersect(r.p, r.u, s.p, s.u);
}
Line q[maxn];
Point p[maxn];
vector<Line> h;
inline int half_plane_intersect() {
sort(h.begin(), h.end());
int n = h.size();
int first, last;
q[first = last = 0] = h[0];
for (int i = 1; i < n; ++ i) {
while (first < last && ! onleft(h[i], p[last - 1])) last --;
while (first < last && ! onleft(h[i], p[first])) first --;
q[++ last] = h[i];
if (parallel(q[last - 1], q[last])) {
last --;
if (onleft(q[last], h[i].p)) q[last] = h[i];
}
if (first < last) p[last - 1] = Line_intersect(q[last], q[last - 1]);
}
while (first < last && ! onleft(q[first], p[last - 1])) first ++;
if (last - first <= 1) return 0;
p[last] = Line_intersect(q[first], q[last]);
return last - first + 1;
}
vector<Point> cvx;
inline bool check(double mid) {
h.clear();
Point a, b;
for (int i = 0; i < n; ++ i) {
a = cvx[i];
b = cvx[(i + 1) % n];
Vector u = zoom(normal(b - a), mid);
h.push_back(Line(a + u, b - a));
}
return half_plane_intersect();
}
int main() {
while (scanf("%d", &n) && n) {
cvx.clear();
for (int i = 0; i < n; ++ i) {
double x, y;
scanf("%lf%lf", &x, &y);
cvx.push_back(Point(x, y));
}
double lf = 0, rg = 20000.0;
while (lf + eps < rg) {
double mid = (lf + rg) / 2.0;
if (check(mid)) lf = mid;
else rg = mid;
}
printf("%.6f\n", lf);
}
}