[POJ3525]Most Distant Point from the Sea(二分+半平面交)

版权声明:转载请注明出处:http://blog.csdn.net/clove_unique https://blog.csdn.net/Clove_unique/article/details/54016078

题目描述

传送门
题意:给出一个多边形,求里面的一个点,其距离离多边形的边界最远,也就是多边形中最大半径圆。

题解

首先二分一个距离
然后将多边形的所有边都往里挪一个距离,求半平面交
如果有交集即为有解
将边向里平移的时候可以应用向量的旋转,然后加加减减

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 1005

const double inf=1e9;
const double pi=acos(-1.0);
const double eps=1e-9;
int dcmp(double x)
{
    if (x<=eps&&x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct Vector
{
    double x,y;
    Vector(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
};
typedef Vector Point;
Vector operator + (Vector a,Vector b) {return Vector(a.x+b.x,a.y+b.y);}
Vector operator - (Vector a,Vector b) {return Vector(a.x-b.x,a.y-b.y);}
Vector operator * (Vector a,double p) {return Vector(a.x*p,a.y*p);}

int n,cnt,ncnt;
double x,y,Max,ans;
Point p[N],poly[N],npoly[N];

double Dot(Vector a,Vector b)
{
    return a.x*b.x+a.y*b.y;
}
double Cross(Vector a,Vector b)
{
    return a.x*b.y-a.y*b.x;
}
double Len(Vector a)
{
    return sqrt(Dot(a,a));
}
Vector rotate(Vector a,double rad)
{
    double sn=sin(rad),cs=cos(rad);
    return Vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
bool insLS(Point A,Point B,Point C,Point D)
{
    Vector v=B-A,w=C-A,u=D-A;
    return dcmp(Cross(v,w))!=dcmp(Cross(v,u));
}
Point GLI(Point P,Vector v,Point Q,Vector w)
{
    Vector u=P-Q;
    double t=Cross(w,u)/Cross(v,w);
    return P+v*t;
}
void init()
{
    cnt=0;
    poly[++cnt]=Point(inf,inf);
    poly[++cnt]=Point(-inf,inf);
    poly[++cnt]=Point(-inf,-inf);
    poly[++cnt]=Point(inf,-inf);
}
void halfp(Point A,Point B)
{
    ncnt=0;
    Point C,D;
    for (int i=1;i<=cnt;++i)
    {
        C=poly[i%cnt+1];
        D=poly[(i+1)%cnt+1];
        if (dcmp(Cross(B-A,C-A))>=0)
            npoly[++ncnt]=C;
        if (insLS(A,B,C,D))
            npoly[++ncnt]=GLI(A,B-A,C,D-C);
    }
    cnt=ncnt;
    for (int i=1;i<=cnt;++i)
        poly[i]=npoly[i];
}
bool check(double mid)
{
    Point A,B,C,D;
    Vector v,w;
    init();
    for (int i=1;i<=n;++i)
    {
        A=p[i%n+1];
        B=p[(i+1)%n+1];
        v=B-A;
        w=rotate(B-A,pi/2.0);
        w=w*(mid/Len(w));
        C=A+w;
        D=C+v;
        halfp(C,D);
    }
    if (cnt) return true;
    else return false;
}
double find()
{
    double l=0,r=Max,mid,ans;
    while (dcmp(r-l)>0)
    {
        mid=(l+r)/2.0;
        if (check(mid)) ans=mid,l=mid;
        else r=mid;
    }
    return ans;
}
int main()
{
    while (~scanf("%d",&n))
    {
        if (!n) break;
        Max=0;
        for (int i=1;i<=n;++i)
        {
            scanf("%lf%lf",&x,&y);
            Max=max(Max,x);Max=max(Max,y);
            p[i]=Point(x,y);
        }
        ans=find();
        printf("%.6lf\n",ans);
    }
    return 0;
}
展开阅读全文

Most Distant Point from the Sea

12-01

DescriptionnThe 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.nIn 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.nInputnThe 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.nn nx1 y1n ⋮ nxn ynnEvery input item in a dataset is a non-negative integer. Two input items in a line are separated by a space.nn 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.nYou 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.nThe last dataset is followed by a line containing a single zero.nOutputnFor 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.nSample Inputn4n0 0n10000 0n10000 10000n0 10000n3n0 0n10000 0n7000 1000n6n0 40n100 20n250 40n250 70n100 90n0 70n3n0 0n10000 10000n5000 5001n0nSample Outputn5000.000000n494.233641n34.542948n0.353553 问答

没有更多推荐了,返回首页