[poj 1113]Wall[凸包]

题意:

求围一座多边形城堡的最短围墙, 围墙需满足: 距离城堡上所有的点至少距离为L

思路:

求出凸包, 边长加上一个半径为L的圆的周长. 因为距离拐点处的最短墙是弧形的.


相当于测试凸包模板了~

#include <cstdio>
#include <cmath>
#include <algorithm>
#define PI acos(-1.0)//精度
using namespace std;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-8;
const double L = 0.618 - 0.1;
const double R = 0.618 + 0.1;
const int MAXN = 10005;

struct point
{
    double x,y;
}res[MAXN],p[MAXN];
int top,n,radius;
/*cross product*/
double cross(point a, point b, point o)//OA X OB
{
    return (a.x - o.x)*(b.y - o.y) - (b.x - o.x)*(a.y - o.y);
}
/*square sum*/
double sqsm(point a, point b)
{
    return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);
}
bool cmp(point a, point b)
{
    if(a.x - b.x > EPS || b.x - a.x > EPS)  return a.x < b.x;
    return a.y < b.y;
}
void Graham()
{
    sort(p, p+n, cmp);
    res[0] = p[0];
    res[1] = p[1];
    top = 1;
    for(int i=2;i<n;i++)
    {
        while(top && cross(p[i], res[top-1], res[top])<=EPS)
            top--;
        res[++top] = p[i];
    }
    int mark = top;
    res[++top] = p[n-2];
    for(int i=n-3;i>=0;i--)
    {
        while(top>mark && cross(p[i], res[top-1], res[top])<=EPS)
            top--;
        res[++top] = p[i];
    }
}

int main()
{
    while(scanf("%d %d",&n,&radius)==2)
    {
        for(int i=0;i<n;i++)
            scanf("%lf %lf",&p[i].x,&p[i].y);
        Graham();
        double ans = 0;
        for(int i=0;i<top;i++)
        {
            ans += sqrt(sqsm(res[i],res[i+1]));
        }
        ans += 2* PI* radius;
        printf("%.0lf\n",ans);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值