Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 3921 | Accepted: 1821 | Special Judge |
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
//AC代码
/* 题意:逆时针给出一个海岛n个点,问你海岛中存在一点距离海岛边界(或大海)最远距离是多少 也可以理解成求在多边形中画一个圆的最大半径(即圆心到圆上的距离(也称半径)) 可以使用半平面交+ 二分法解。对每个距离进行判定(是否存在区域可以放置圆心,多边形的的每条边向内推进 mid距离 一直推进到圆心 */ #include<iostream> #include<queue> #include<cstdio> #include<algorithm> #include<cstring> #include<iomanip> #include<map> #include<cstdlib> #include<cmath> #include<vector> const double eps=1e-8; const double PI=acos(-1.0); const double e=2.7182818284590452354; const double inf=1e8; const int INF=0x7fffffff; const int Max=1001; #define zero(x) (((x)>0?(x):-(x))<eps) #define mm(a,b) memset(a,b,sizeof(a)) using namespace std; int sign(double x) { return (x>eps)-(x<-eps); } typedef struct Node { double x; double y; Node(const double &_x=0,const double &_y=0):x(_x),y(_y){} void input() { cin>>x>>y; } void output() { cout<<x<<" "<<y<<endl; } }point; point list[Max],stack[Max]; point qq[Max],pp[Max],pnt[Max]; int n; int top; int cnt; int curcnt; double xmult(point p0,point p1,point p2) { return(p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } double Distance(point p1,point p2)// 返回两点之间欧氏距离 { return( sqrt( (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y) ) ); } bool cmp(point p1,point p2) { double temp; temp=xmult(list[0],p1,p2); if(temp>0) return true; if(temp==0&&(Distance(list[0],p1)<Distance(list[0],p2))) return true; return false; } void Init(int n) { int i; for(i=1;i<=n;i++) { pp[i]=pnt[i]; } pp[n+1]=pnt[1]; pp[0]=pnt[n]; cnt=n; } void GetLine(point u,point v,double &a,double &b,double &c) { a=v.y-u.y; b=u.x-v.x; c=v.x*u.y-u.x*v.y; } point Intersect(point u,point v,double a,double b,double c) { double q=fabs(a*u.x+b*u.y+c); double p=fabs(a*v.x+b*v.y+c); point res; res.x=((p*u.x+q*v.x)/(q+p)); res.y=((p*u.y+q*v.y)/(q+p)); return res; } void CutLine(double a,double b,double c) { int i; curcnt=0; for(i=1;i<=cnt;i++) { if(sign(a*pp[i].x+b*pp[i].y+c)>=0) { qq[++curcnt]=pp[i]; } else { if(sign(a*pp[i-1].x+b*pp[i-1].y+c)>0) { qq[++curcnt]=Intersect(pp[i],pp[i-1],a,b,c); } if(sign(a*pp[i+1].x+b*pp[i+1].y+c)>0) { qq[++curcnt]=Intersect(pp[i],pp[i+1],a,b,c); } } } for(i=1;i<=curcnt;i++) { pp[i]=qq[i]; } pp[curcnt+1]=pp[1]; pp[0]=pp[curcnt]; cnt=curcnt; } bool solve(double r) { Init(n); int i; double kk,a,b,c; point ta,tb,tt; for(i=1;i<=n;i++) { tt.x=pnt[i+1].y-pnt[i].y; tt.y=pnt[i].x-pnt[i+1].x; kk=r/sqrt(tt.x*tt.x+tt.y*tt.y); tt.x=tt.x*kk; tt.y=tt.y*kk; ta.x=pnt[i].x+tt.x; ta.y=pnt[i].y+tt.y; tb.x=pnt[i+1].x+tt.x; tb.y=pnt[i+1].y+tt.y; //cout<<"-------------"<<endl; //cout<<ta.x<<" "<<ta.y<<" "<<tb.x<<" "<<tb.y<<endl; //cout<<"--------------"<<endl; GetLine(ta,tb,a,b,c); CutLine(a,b,c); } if(cnt<=0) return false; return true; } int main() { int m,i,j; double low,hign,mid; while(cin>>n&&n) { for(i=1;i<=n;i++) pnt[i].input(); for(i=1;i<(n+1)/2;i++)//因为输入时候是逆时针顺序所以要改成顺时针 swap(pnt[i],pnt[n-i]); pnt[n+1]=pnt[1]; hign=inf; low=0; while(low+eps<=hign) { mid=(hign+low)/2.0; //cout<<low<<" "<<hign<<" "<<mid<<endl; if(solve(mid)) low=mid; else hign=mid; } cout<<setprecision(6)<<setiosflags(ios::fixed)<<hign<<endl; } return 0; }