poj 1113 Wall

题意:国王要建一座墙围住国王所有的城堡,要求墙上任意一点到城堡的距离大于L,同时墙的长度最短。输出墙的长度(四舍五入)。
墙的长度 = 城堡的点构成的凸包的长度 + 以L为半径的圆的周长

这里写图片描述
黑色的是城堡连接成的凸包,外围黄色的部分是墙,顶点上 是圆弧

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int Max = 1005;
const double PI = 3.141592653;

struct Point{
    double x, y;
}p[Max];
int n, res[Max], top;

bool cmp(struct Point a, struct Point b){
    if(a.y == b.y) return a.x < b.x;
    return a.y < b.y;
}

double mult(Point sp, Point ep, Point op){
    return (sp.x-op.x)*(ep.y-op.y) - (ep.x-op.x)*(sp.y-op.y);
}

double mydis(int i, int j){
    double a = p[i].x - p[j].x;
    double b = p[i].y - p[j].y;
    return sqrt(a*a + b*b);
}

void Graham(){
    int i, len;
    top = 1;
    sort(p, p+n, cmp);//p[0]是最左下的点,p[n-1]是最右上的点
    for(i = 0; i < 3; i ++) res[i] = i;
    for(i = 2; i < n; i ++){
        while(top && mult(p[i], p[res[top]], p[res[top-1]]) >= 0) top --;
        res[++ top] = i;
    }//凸包画了下面一半
    len = top;
    res[++ top] = n-2;
    for(i = n - 3; i >= 0; i --){
        while(top != len && mult(p[i], p[res[top]], p[res[top-1]]) >= 0) top --;
        res[++ top] = i;
    }//剩下一半
}

int main(){
    int i;
    double r, ans;
    scanf("%d%lf", &n, &r);
    for(i = 0; i < n; i ++)
        scanf("%lf%lf", &p[i].x, &p[i].y);
    Graham();
    ans = 2 * PI * r;
    for(i = 0; i < top-1; i ++)
        ans += mydis(res[i], res[i+1]);
    ans += mydis(res[0], res[top-1]);
    printf("%.lf\n", ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值