poj - 1113 - Wall

本文介绍了一种计算给定点集凸包周长的方法,并在此基础上计算向外扩展特定距离后的图形周长。通过使用C++实现的凸包算法,结合点之间的距离计算和圆的周长公式,最终得到精确的周长结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:顺时针方向给出N个点,求外围距离这些点L距离的点围成的图形的周长,结果四舍五入到整数(3 <= N <= 1000,1 <= L <= 1000,-10000 <= Xi, Yi <= 10000) 。

题目链接:http://poj.org/problem?id=1113

——>>先求凸包,然后求凸包的周长加上一个半径为L的圆的周长。

注意:用round()四舍五入后,若用%.0lf输出会WA,round()后强转为int后输出AC!

#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

const int maxn = 1000 + 10;
const double eps = 1e-10;
const double pi = acos(-1);

int dcmp(double x){
    if(fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}

struct Point{
    double x;
    double y;
    Point(double x = 0, double y = 0):x(x), y(y){}
    bool operator < (const Point& e) const{
        return x < e.x || (dcmp(x - e.x) == 0 && y < e.y);
    }
}p[maxn], q[maxn];

typedef Point Vector;

Vector operator + (Point A, Point B){
    return Vector(A.x + B.x, A.y + B.y);
}

Vector operator - (Point A, Point B){
    return Vector(A.x - B.x, A.y - B.y);
}

Vector operator * (Point A, double p){
    return Vector(A.x * p, A.y * p);
}

Vector operator / (Point A, double p){
    return Vector(A.x / p, A.y / p);
}

double Cross(Vector A, Vector B){
    return A.x * B.y - B.x * A.y;
}

int ConvexHull(Point *p, int n, Point* ch){        //求凸包
    sort(p, p + n);
    int m = 0;
    for(int i = 0; i < n; i++){
        while(m > 1 && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) < 0) m--;
        ch[m++] = p[i];
    }
    int k = m;
    for(int i = n-2; i >= 0; i--){
        while(m > k && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) < 0) m--;
        ch[m++] = p[i];
    }
    if(n > 1) m--;
    return m;
}

double Dis(Point A, Point B){
    return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
}

int main()
{
    int N, L;
    while(scanf("%d%d", &N, &L) == 2){
        for(int i = 0; i < N; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
        double ret = 0;
        int m = ConvexHull(p, N, q);
        for(int i = 1; i < m; i++) ret += Dis(q[i], q[i-1]);
        ret += Dis(q[0], q[m-1]) + 2 * pi * L;
        int ans = round(ret);
        printf("%d\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值