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 |
题目:http://poj.org/problem?id=3525
题意:给你一个岛屿,求这个岛屿中离海最远的点的距离,岛屿是个多边形
分析:这题我没能想出来怎么做,尽管已经看过提示了= =,后来看别人的题解才恍然大悟!
首先,二分这个距离R,然后用半平面交来判断是否存在这样的点,具体就是把每条边往岛内移动R,求移动后半平面交是否存在解,PS,这题我求了下面积反而精度不够了,直接判断剩余半平面个数就行
代码:
#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int mm=111;
typedef double diy;
struct point
{
diy x,y;
point(){}
point(diy _x,diy _y):x(_x),y(_y){}
}g[mm];
point Vector(point s,point t)
{
return point(t.x-s.x,t.y-s.y);
}
diy CrossProduct(point P,point Q)
{
return P.x*Q.y-P.y*Q.x;
}
diy MultiCross(point P,point Q,point R)
{
return CrossProduct(Vector(Q,P),Vector(Q,R));
}
struct halfPlane
{
point s,t;
diy angle;
halfPlane(){}
halfPlane(point _s,point _t){s=_s,t=_t,angle=atan2(t.y-s.y,t.x-s.x);}
}hp[mm],q[mm],o[mm];
point Intersection(halfPlane P,halfPlane Q)
{
diy a1=CrossProduct(Vector(P.s,Q.t),Vector(P.s,Q.s));
diy a2=CrossProduct(Vector(P.t,Q.s),Vector(P.t,Q.t));
return point((P.s.x*a2+P.t.x*a1)/(a1+a2),(P.s.y*a2+P.t.y*a1)/(a1+a2));
}
bool IsParallel(halfPlane P,halfPlane Q)
{
return fabs(CrossProduct(Vector(P.s,P.t),Vector(Q.s,Q.t)))<1e-8;
}
bool cmp(halfPlane P,halfPlane Q)
{
if(fabs(P.angle-Q.angle)<1e-8)
return MultiCross(P.s,P.t,Q.s)>0;
return P.angle<Q.angle;
}
void HalfPlaneIntersect(int n,int &m)
{
sort(hp,hp+n,cmp);
int i,l=0,r=1;
for(m=i=1;i<n;++i)
if(hp[i].angle-hp[i-1].angle>1e-8)hp[m++]=hp[i];
n=m;
m=0;
q[0]=hp[0],q[1]=hp[1];
for(i=2;i<n;++i)
{
if(IsParallel(q[r],q[r-1])||IsParallel(q[l],q[l+1]))return;
while(l<r&&MultiCross(hp[i].s,hp[i].t,Intersection(q[r],q[r-1]))>0)--r;
while(l<r&&MultiCross(hp[i].s,hp[i].t,Intersection(q[l],q[l+1]))>0)++l;
q[++r]=hp[i];
}
while(l<r&&MultiCross(q[l].s,q[l].t,Intersection(q[r],q[r-1]))>0)--r;
while(l<r&&MultiCross(q[r].s,q[r].t,Intersection(q[l],q[l+1]))>0)++l;
q[++r]=q[l];
for(i=l;i<r;++i)
g[m++]=Intersection(q[i],q[i+1]);
}
int n;
halfPlane Move(halfPlane P,double R)
{
P.angle+=acos(-1.0)/2;
P.s.x+=R*cos(P.angle);
P.s.y+=R*sin(P.angle);
P.t.x+=R*cos(P.angle);
P.t.y+=R*sin(P.angle);
return P;
}
bool Check(double R)
{
int i,m;
double ret=0;
for(i=0;i<n;++i)
hp[i]=Move(o[i],R);
HalfPlaneIntersect(n,m);
return m>2;
}
int main()
{
int i;
double l,r,mid;
while(scanf("%d",&n),n)
{
for(i=0;i<n;++i)
scanf("%lf%lf",&g[i].x,&g[i].y);
g[n]=g[0];
for(i=0;i<n;++i)
o[i]=halfPlane(g[i],g[i+1]);
l=0,r=100001;
while(r-l>1e-7)
{
mid=(l+r)/2;
if(Check(mid))l=mid;
else r=mid;
}
printf("%.6lf\n",l);
}
return 0;
}