【杭电oj】1348 - Wall (凸包)

题目大意:
给出一些点,求距离这些点形成的凸包的端点L的图形的周长。
即凸包的周长加上半径为L的园的周长,因为该图形在端点处一定是形成圆角,因为封闭,故圆角转过360度形成了一个圆。

AC代码:

#include<iostream>
#include<algorithm> 
#include<math.h>
#include<stdio.h>
#define pi acos(-1.0)
using namespace std;

struct Point{
    double x,y;
    Point(){}
    Point(int xx,int yy){
        x = xx;
        y = yy;
    }
    Point operator - (const Point a)const{
        return Point(x-a.x,y-a.y);
    }
    Point operator * (const Point a)const{
        return Point();
    }
}p[1100],ch[1100];

bool cmp(Point a,Point b){
    if(a.x == b.x)  return a.y < b.y;
    return a.x < b.x; 
}
double det (Point a, Point b) {
    return a.x * b.y - a.y * b.x;
}

int ConvexHull ( Point * p,int n, Point * ch ){
    sort(p,p+n,cmp); 
    int m=0;
    for (int i=0;i<n;i ++){
        while (m >1&& det(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 && det(ch[m -1] - ch[m -2] ,
            p[i]-ch[m -2]) <=0) m --;
        ch[m ++]= p[i];
    }
    if(n >1) m--;
    return m;
}

int main(void){
    int n,m,T;
    cin>>T;
    while(T--){
        double r,ans = 0;
        cin>>n>>r;
        for(int i=0;i<n;i++)    cin>>p[i].x>>p[i].y;
        m = ConvexHull(p,n,ch);
        for(int i=0;i<m;i++){
            ans += sqrt((ch[i].x-ch[(i+1)%m].x)*(ch[i].x-ch[(i+1)%m].x)+
            (ch[i].y-ch[(i+1)%m].y)*(ch[i].y-ch[(i+1)%m].y));
        }
    //  for(int i=0;i<m;i++)    cout<<ch[i].x<<' '<<ch[i].y<<endl;
        ans += pi*r*2;
        printf("%.0lf\n",ans);
        if(T)   cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41009682/article/details/81268795
今日推荐