您的位置:首页 > 其它

POJ 1113 && HDU 1348 Wall(凸包)

2015-06-20 16:48 459 查看
Description

给定多边形城堡的n个顶点,绕城堡外面建一个围墙,围住所有点,并且墙与所有点的距离至少为L,求这个墙最小的长度

Input

第一行两个整数n,l(n,l<=1000)分别表示定点个数和墙与城堡最短距离,以后n行每行两个整数x,y(-10000<=x,y<=10000)

Output

墙的最短长度(结果四舍五入)

Sample Input

9 100

200 400

300 400

300 300

400 300

400 400

500 400

500 200

350 200

200 200

Sample Output

1628

Solution

城墙最短=凸包的周长 + 以L为半径的圆的周长。

首先,我们得出城墙最短=凸包的周长+相应顶点的弧长(半径都为L)之和。

再证明:相应顶点的弧长(半径都为L)之和=以L为半径的圆的周长。

事实上,设凸包顶点为n,n个顶点组成n个周角,角度为360*n=2*180*n,凸包的内角和为180*(n-2),作了2*n条垂线,和为2*n*90=180*n,所以总圆周角为2*180*n-180*(n-2)-180*n=360,组成圆

Code

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef struct
{
double x;//横坐标
double y;//纵坐标
double k;//与第一个定点连线的正切值
}point;
point a[50005];
point b[50005];
int n,res;
double multiply(point x1,point x2,point x3)//叉乘
{
return ((x3.y-x2.y)*(x2.x-x1.x)-(x2.y-x1.y)*(x3.x-x2.x));
}
double distance(point x1,point x2)//两点间间距
{
double c;
c=(x1.x-x2.x)*(x1.x-x2.x)+(x1.y-x2.y)*(x1.y-x2.y);
return sqrt(c);
}
int compare1(const void*x1,const void*x2)//按坐标排序
{
point *x3,*x4;
x3=(point*)x1;
x4=(point*)x2;
if(x3->y==x4->y)
return x3->x>x4->x?1:-1;
else
return x3->y>x4->y?1:-1;
}
int compare2(const void*x1,const void*x2)//按斜率排序
{
point *x3,*x4;
x3=(point*)x1;
x4=(point*)x2;
if(x3->k==x4->k)
{
if(x3->x==x4->x)
return x4->y>x3->y?1:-1;
else
return x4->x>x3->x?1:-1;
}
else
return x3->k>x4->k?1:-1;
}
int main()
{
int i,j,t,flag1,flag2;
double dis,len=0;
float r;
while(scanf("%d",&n)!=EOF)
{
scanf("%f",&r);
for(i=0;i<n;i++)
scanf("%lf%lf",&a[i].x,&a[i].y);
qsort(&a[0],n,sizeof(double)*3,compare1);//对顶点排序
for(i=1;i<n;i++)
{
a[i].k=atan((double)((a[i].y-a[0].y)/(a[i].x-a[0].x)));
if(a[i].k<(double(0)))//若正切值小于0则加PI
a[i].k+=acos((double)-1);
}
qsort(&a[1],n-1,sizeof(double)*3,compare2);//对斜率排序
b[0]=a[0];
b[1]=a[1];
b[2]=a[2];
res=3;
for(i=3;i<n;i++)
{
while(res>2&&multiply(b[res-2],b[res-1],a[i])<=0)//若加入新点后破坏凸性则将凹的部分的点从末尾除去
res--;
b[res++]=a[i];//加入新点
}
for(i=0;i<res-1;i++)//求凸包周长
len+=distance(b[i],b[i+1]);
len+=distance(b[res-1],b[0]);//加上起点与终点间距
len+=2*r*acos((double)-1);//以r为半径的圆的周长
printf("%.f\n",len);//四舍五入输出
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: