题意
n个点构成的城堡,给出每个点的坐标。若要修建距离城堡最近距离为L的城墙,问城墙的最短长度。
思路
就是求这个多边形的凸包加上一个半径为L的圆的周长
凸包模板题目。
代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define PI atan(1.0)*4
#define eps 1e-8
#define N 1050
struct Node
{
double x,y;
}p[N],q[N];
double cross(Node a,Node b,Node c)//向量ab*向量ac
{
return ((b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x));
}
double dis(Node a,Node b)//a点与b点的距离的平方)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cmp(Node a,Node b)//把点按以p0为中心的极角逆时针排序
{
if(cross(p[0],a,b)>0)//向量p[0]a在向量p[0]b顺时针方向
return 1;
if(cross(p[0],b,a)==0&&(dis(p[0],b)-dis(p[0],a)>eps))//p[0]a与p[0]b共线,且b点距p[0]更远
return 1;
return 0;
}
int Graham(int n)
{
int top=2;
sort(p+1,p+n,cmp);
q[0]=p[0];
q[1]=p[1];
q[2]=p[2];
for(int i=3;i<n;i++){
while(top>=1&&cross(q[top-1],p[i],q[top])>=0) //
top--;
q[++top]=p[i];
}
return top;
}
int main()
{
int t,n,l;
scanf("%d%d",&n,&l);
for(int i=0; i<n; i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
int k=0;
for(int i=0; i<n; i++){
if(p[k].y>p[i].y||(p[k].y==p[i].y&&p[k].x>p[i].x))
k=i;
}
swap(p[0],p[k]);
int K=Graham(n);
double sum=2*PI*l+sqrt(dis(q[0],q[K]));
for(int i=1; i<=K; i++)
sum+=sqrt(dis(q[i],q[i-1]));
printf("%.f\n",sum);
return 0;
}