Description
Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King's requirements.
The task is somewhat simplified by the fact, that the King's castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle's vertices in feet.
Input
Next N lines describe coordinates of castle's vertices in a clockwise order. Each line contains two integer numbers Xi and Yi separated by a space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All vertices are different and the sides of the castle do not intersect anywhere except for vertices.
Output
Sample Input Sample Output
题意:
要将所有点围成一个类似于矩形的形状,角变成弧。
也就是求凸包周长+一个完整的圆周长。
因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆。
剩余的部分就是凸包的周长。
具体如下图。
思路:
先求出凸包,然后求一个周长。注意四舍五入!!!
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define PI 3.1415926535
using namespace std;
struct node
{
int x,y;
};
node vex[];//存入的所有的点
node stackk[];//凸包中所有的点
bool cmp1(node a,node b)
{
if(a.y==b.y)
return a.x<b.x;
else
return a.y<b.y;
}
int cross(node a,node b,node c)//计算叉积
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
double dis(node a,node b)//计算距离
{
return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));
}
bool cmp(node a,node b)//极角排序
{
int m=cross(vex[],a,b);
if(m==)
return dis(vex[],a)-dis(vex[],b)<=?true:false;
else
return m>?true:false;
}
int main()
{
int t,L;
while(~scanf("%d%d",&t,&L))
{
int i;
for(i=; i<t; i++)
{
scanf("%d%d",&vex[i].x,&vex[i].y);
}
if(t==)
printf("%.2f\n",0.00);
else if(t==)
printf("%.2f\n",dis(vex[],vex[]));
else
{
sort(vex,vex+t,cmp1);
sort(vex+,vex+t,cmp);
memset(stackk,,sizeof(stackk));
stackk[]=vex[];
stackk[]=vex[];
int top=;//最后凸包中拥有点的个数
for(i=; i<t; i++)
{
while(i>=&&cross(stackk[top-],stackk[top],vex[i])<)
top--;
stackk[++top]=vex[i];
}
double s=;//注意精度
for(i=; i<=top; i++)
s+=dis(stackk[i-],stackk[i]);
s+=dis(stackk[top],vex[]);
s+=*PI*L;
int ans=s+0.5;//四舍五入
printf("%d\n",ans);
}
}
}