nyoj 226 wall (凸包 Graham算法)

Graham 算法

围墙的长途为凸包长+最近距离为半径的圆的周长

#include<stdio.h>
#include<math.h>
#include<algorithm>
#define pi 3.1415926
using namespace std;
int n,L,top;
struct point 
{	
	int x,y;
}p[1005],s[1005],t;
double cross(point t1,point t2,point t3,point t4)  //求向量t1t2和向量t3t4的叉积
{	
	return (t2.x-t1.x)*(t4.y-t3.y)-(t2.y-t1.y)*(t4.x-t3.x);
}
double dis(point t1,point t2)   //求距离
{
	double z=(t2.x-t1.x)*(t2.x-t1.x)+(t2.y-t1.y)*(t2.y-t1.y);
	return sqrt(z);
}
bool cmp(point t1,point t2)
{	
	double z=cross(p[0],t1,p[0],t2);
	return z?z>0:dis(p[0],t1)>dis(p[0],t2);
 }
void findpoint()    //找基点,按y从小到大,如果y相同,按x从左到右
{   int i,j=0;	
    t=p[0];
	for(i=1;i<n;i++)
	{	
		if(t.y>p[i].y||(t.y==p[i].y&&t.x>p[i].x))	
		{		
			j=i;		
			t=p[i];	
		}
	}
	t=p[0];	
	p[0]=p[j];
	p[j]=t;
}
void scanner()
{	
	int i;
	findpoint();
	sort(p+1,p+n,cmp);
	s[0]=p[0];
	s[1]=p[1];
	top=1;	
	for(i=2;i<n;i++)
	{	
		while(cross(s[top-1],s[top],s[top],p[i])<0)	
			top--;	
		top++;	
		s[top]=p[i];
	}
}
int main()
{	
	int i;
	double ans;
	while(~scanf("%d%d",&n,&L))	
	{	
		for(i=0;i<n;i++)
		{	
			scanf("%d%d",&p[i].x,&p[i].y);	
		}	
		scanner();	
		ans=2*pi*L;	
		for(i=0;i<=top;i++)	
		{			
			ans+=dis(s[i],s[(i+1)%(top+1)]);
		}	
		printf("%.0lf\n",ans);
	}
	return 0;
}
        


 

你可能感兴趣的:(nyoj 226 wall (凸包 Graham算法))