POJ1113---Wall(基础计算几何:凸包入门)

【题目来源】http://poj.org/problem?id=1113
【题意】
国王给你一些点,这些点组成一个城堡,国王要求在这个多边形形状的城堡周围建起周长最小的城墙,并且城墙与城堡的距离处处不得小于L。
【思路】
凸包模板。只需要求一下这些点构成的凸包+以题目上给的距离为半径的周长。
【代码】

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const double eps=1e-6;
const double PI=acos(-1.0);
struct point
{
    double x,y;
};
int sgn(double x)//精度
{
    if(fabs(x)<eps) return 0;
    if(x<0) return -1;
    return 1;
}
double operator*(point a,point b)//点积
{
    return a.x*b.x+a.y*b.y;
}
double operator^(point a,point b)//叉积
{
    return a.x*b.y-a.y*b.x;
}
point operator-(point a,point b)//减法
{
    point c;
    c.x=a.x-b.x;
    c.y=a.y-b.y;
    return c;
}
double dis(point a,point b)//求距离
{
    return sqrt((a-b)*(a-b));
}
point s[1010];
bool cmp(point a,point b)//极角坐标排序利用叉积的几何性质
{
    double tmp=(a-s[0])^(b-s[0]);
    if(sgn(tmp)>0||(sgn(tmp)==0&&dis(a,s[0])<dis(b,s[0]))) return 1;
    return 0;
}
int t[1010];
int top;
void Graham(int n)//凸包的算法
{
    if(n==1)
    {
        top=0;
        t[0]=0;
    }
    else if(n==2)
    {
        top=1;
        t[0]=0;
        t[1]=1;
    }
    else if(n>2)
    {
        for(int i=0; i<=1; i++) t[i]=i;
        top=1;
        for(int i=2; i<n; i++)
        {
            while(top>0&&((s[t[top]]-s[t[top-1]])^(s[i]-s[t[top-1]]))<=0) top--;
            t[++top]=i;
        }
    }
}
int main()
{
    int n,r;
    scanf("%d%d",&n,&r);
    scanf("%lf%lf",&s[0].x,&s[0].y);
    for(int i=1; i<n; i++)
    {
        scanf("%lf%lf",&s[i].x,&s[i].y);
        if(s[i].y<s[0].y||s[i].y==s[0].y&&s[i].x<s[0].x)
            swap(s[i],s[0]);
    }
    sort(s+1,s+n,cmp);
    Graham(n);
    double res=0;
    for(int i=0; i<top; i++)
        res+=dis(s[t[i]],s[t[i+1]]);
    res+=dis(s[t[0]],s[t[top]]);
    res+=2*PI*(r*1.0);
    printf("%d\n",(int) (res+0.5));
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值