您的位置:首页 > 其它

Wall(凸包)

2013-10-25 21:06 99 查看
http://poj.org/problem?id=1113

题意:给出一些点的坐标,和一个半径r,求出这些点围成的凸包的周长再加上一个半径为r的圆的周长。

#include <stdio.h>
#include <algorithm>
#include <math.h>
const double PI=acos(-1.0);
const int N=1002;
using namespace std;

struct Point
{
double x;
double y;
Point (double x = 0,double y = 0):x(x),y(y) {}
bool friend operator < (const Point &a, const Point &b)
{
return a.x < b.x||(a.x==b.x&&a.y < b.y);
}
} p
,c
;
typedef Point Vector;
double dis(Point A,Point B)//两点间的距离
{
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
Vector operator- (Point A,Point B)//求向量
{
return Vector(B.x-A.x,B.y-A.y);
}
double Cross(Vector A,Vector B)//求叉积
{
return A.x*B.y-A.y*B.x;
}
int ConvexHull(Point *p,int n,Point *c)//计算凸包
{
int k = 0;
for (int i = 0; i < n; i++)
{
while(k > 1 && Cross(c[k-2]-c[k-1],c[k-2]-p[i])<=0)
k--;
c[k++] = p[i];

}
int m = k;
for (int i = n-2; i >= 0; i--)
{
while(k > m && Cross(c[k-2]-c[k-1],c[k-2]-p[i])<=0)
k--;
c[k++] = p[i];
}
c[k] = c[0];
return k;
}

int main()
{
int n;
double r;
scanf("%d %lf",&n,&r);
for (int i = 0; i < n; i++)
scanf("%lf %lf",&p[i].x,&p[i].y);
sort(p,p+n);
int cnt = ConvexHull(p,n,c);
double len = 0;
for (int i = 1; i <= cnt; i++)
{
len+=dis(c[i],c[i-1]);
}
len+=2*PI*r;
printf("%.0f",len);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: