POJ3525:Most Distant Point from the Sea——题解

http://poj.org/problem?id=3525

题目大意:给一个逆时针序列的多边形点集,求其中可以画的最大半径的圆的半径。

——————————————————————

二分枚举半径长度,然后将所有的边往内缩半径为r,求是否有内核即可。

#include<cstdio>
#include<queue>
#include<cctype>
#include<cstring>
#include<stack>
#include<cmath>
#include<algorithm>
using namespace std;
typedef double dl;
const dl eps=1e-10;
const int N=301;
struct Point{
    dl x;
    dl y;
}p[N],point[N],q[N],z;
//point,初始点 
//q,暂时存可行点
//p,记录可行点 
int n,curcnt,cnt;
//curcnt,暂时存可行点个数 
//cnt,记录可行点个数
inline Point getmag(Point a,Point b){
    Point s;
    s.x=b.x-a.x;s.y=b.y-a.y;
    return s;
}
inline dl multiX(Point a,Point b){
    return a.x*b.y-b.x*a.y;
}
inline void getline(Point x,Point y,dl &a,dl &b,dl &c){
    a=y.y-x.y;
    b=x.x-y.x;
    c=y.x*x.y-x.x*y.y;
    return;
}
inline Point intersect(Point x,Point y,dl a,dl b,dl c){
    Point s;
    dl u=fabs(a*x.x+b*x.y+c);  
    dl v=fabs(a*y.x+b*y.y+c);
    s.x=(x.x*v+y.x*u)/(u+v);
    s.y=(x.y*v+y.y*u)/(u+v);
    return s;  
} 
inline void cut(dl a,dl b,dl c){
    curcnt=0;
    for(int i=1;i<=cnt;i++){
        if(a*p[i].x+b*p[i].y+c>-eps)q[++curcnt]=p[i];
        else{
            if(a*p[i-1].x+b*p[i-1].y+c>eps){
                q[++curcnt]=intersect(p[i],p[i-1],a,b,c);
        }
            if(a*p[i+1].x+b*p[i+1].y+c>eps){
                q[++curcnt]=intersect(p[i],p[i+1],a,b,c);
        }
        }
    }
    for(int i=1;i<=curcnt;i++)p[i]=q[i];
    p[curcnt+1]=p[1];p[0]=p[curcnt];
    cnt=curcnt;
    return;
}
inline void init(){
    for(int i=1;i<=n;i++)p[i]=point[i];
    z.x=z.y=0;
    p[n+1]=p[1];
    p[0]=p[n];
    cnt=n;
    return;
}
inline void regular(){//调换方向 
    for(int i=1;i<(n+1)/2;i++)swap(point[i],point[n-i]);
    return;
}
inline bool solve(dl r){
    init();
    for(int i=1;i<=n;i++){
    Point ta,tb,tt;
    tt.x=point[i+1].y-point[i].y;
    tt.y=point[i].x-point[i+1].x;
    dl k=r/sqrt(tt.x*tt.x+tt.y*tt.y);
    tt.x*=k;tt.y*=k;
    ta.x=point[i].x+tt.x;
    ta.y=point[i].y+tt.y;
    tb.x=point[i+1].x+tt.x;
    tb.y=point[i+1].y+tt.y;
        dl a,b,c;
        getline(ta,tb,a,b,c);
        cut(a,b,c);
    }
    return cnt;
}
int main(){
    while(scanf("%d",&n)!=EOF&&n){
        for(int i=1;i<=n;i++){
            scanf("%lf%lf",&point[i].x,&point[i].y);
        }
    regular();
    point[n+1]=point[1];
    dl l=0,r=10000000;
    while(fabs(l-r)>eps){
        dl mid=(l+r)/2.0;
        if(solve(mid))l=mid;
        else r=mid;
    }
       printf("%.6f\n",l);
    }
    return 0;
}

 

posted @ 2017-12-26 15:54  luyouqi233  阅读(448)  评论(0编辑  收藏  举报