POJ3525/LA3890 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.
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
解题报告
这道题的题意是:给我们一个凸多边形(也就是一个半平面交),问我们这其中哪一个点离最近的那个边框最远(也就是离整个边框最远)。
我们的解法是这样的:二分出一个值,将每一条线段的一个端点不断地缩小这个二分出的值,check的时候就检验半平面交是否为空。
不过,这份代码在POJ上会被卡精度,然而在LA上就可以过
代码如下

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define Point Vector
using namespace std;
const double eps=1e-8;
const int N=1e5;
int sign(double a){return a>eps?1:(a<-eps?-1:0);}
struct Vector
{
    double x,y;
    double len(){return sqrt(x*x+y*y);}
    double ang(){return atan2(y,x);}
    Vector(double a=.0,double b=.0):x(a),y(b){}
    Vector operator+(const Vector &s){return Vector(x+s.x,y+s.y);}
    Vector operator-(const Vector &s){return Vector(x-s.x,y-s.y);}
    Vector operator*(double s){return Vector(x*s,y*s);}
    Vector operator/(double s){return Vector(x/s,y/s);}
};
Vector Normal(Vector a){double L=a.len();return Vector(-a.y/L,a.x/L);}//算出一个向量的单位向量的负值 
struct Line
{
    Point p;
    Vector u;
    double ang;
    Line(){}
    Line(Point p,Vector v):p(p),u(v),ang(u.ang()){}
    bool operator<(const Line &s){return ang<s.ang;}
    bool operator==(const Line &s){return sign(ang-s.ang)==0;}
    bool operator<(const Line&L)const{return ang<L.ang;}
};
double dot(const Vector &a,const Vector &b){return a.x*b.x+a.y*b.y;}//向量点积 
double cross(const Vector &a,const Vector &b){return a.x*b.y-a.y*b.x;}//向量叉积
Point line_intersect(Point P,Vector u,Point Q,Vector v) 
{
    double t=cross(Q-P,v)/cross(u,v);
    return P+u*t;
}
Point line_intersect(const Line &a,const Line &b)//求出两直线交点 
{
    return line_intersect(a.p,a.u,b.p,b.u);
}
bool seg_intersect(Point A,Point B,Point C,Point D)//判定两线段(AB,CD)是否相交 
{
    double c1=cross(B-A,C-A),c2=cross(B-A,D-A);
    double c3=cross(D-C,A-C),c4=cross(D-C,B-C);
    return sign(c1)*sign(c2)<0&&sign(c3)*sign(c4)<0;
}
bool cmp1(Line r,Line s)
{
    return r<s;
}
bool onleft(Point a,Point b,Point p)
{
    return sign(cross(b-a,p-a))>0;
}
bool onleft(Line l,Point p)
{
    return sign(cross(l.u,p-l.p))>0;
}
vector<Point>half_plane_intersect(vector<Line>L)
{
    int n=L.size();
    sort(L.begin(),L.end());
    int first,last;
    vector<Point> p(n);
    vector<Line> q(n);
    vector<Point> ans;
    q[first=last=0]=L[0];
    for(int i=1;i<n;i++)
    {
        while(first<last&&!onleft(L[i],p[last-1]))last--;
        while(first<last&&!onleft(L[i],p[first]))first++;
        q[++last]=L[i];
        if(fabs(cross(q[last].u,q[last-1].u))<eps)
        {
            last--;
            if(onleft(q[last],L[i].p))q[last]=L[i];
        }
        if(first<last)p[last-1]=line_intersect(q[last-1],q[last]);
    }
    while(first<last&&!onleft(q[first],p[last-1]))last--;
    if(last-first<=1)return ans;
    p[last]=line_intersect(q[last],q[first]);
    for(int i=first;i<=last;i++)ans.push_back(p[i]);
    return ans;
}
int n;
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        vector<Point>p,v,normal;
        for(int i=1;i<=n;i++)
        {   
            int x,y;
            scanf("%d%d",&x,&y);
            p.push_back(Point(x,y));

        }
        for(int i=0;i<=n-1;i++)
        {
            v.push_back(p[(i+1)%n]-p[i]);
            normal.push_back(Normal(v[i]));//存储每个向量的单位向量的负值 
        }
        double lf=0,rg=20000;
        while(rg-lf>eps)
        {
            vector<Line>h;
            double mid=lf+(rg-lf)/2.0;
            for(int i=0;i<=n-1;i++)
            h.push_back(Line(p[i]+normal[i]*mid,v[i]));
            vector<Point>poly=half_plane_intersect(h);//返回二分后的半平面交 
            if(poly.empty())rg=mid;
            else lf=mid;
        }
        printf("%.6lf\n",lf);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值