poj 113——wall

来源:互联网 发布:淘宝样式管理 编辑:程序博客网 时间:2024/06/04 01:37

凸包

凸包长度+2*pi*r

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;#define eps (1e-8)#define pi acos(-1.0)struct point{double x,y;point(double x=0, double y=0) : x(x), y(y){}}p[1100],ans[1100];typedef point vector;point operator-(point a,point b) { return point(a.x-b.x, a.y-b.y);};int n;double r;double Len(point& a,point& b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}inline int Sgn(double x) {return x < -eps ? -1 : x > eps ? 1 : 0;}bool Cmp(point& a,point& b){if(Sgn(a.x-b.x)==0)return a.y<b.y;elsereturn a.x<b.x;}int Cross(vector a,vector b){return Sgn(a.x*b.y-a.y*b.x);}int Andrew(){sort(p,p+n,Cmp);int m=0;           //凸包上的点的个数 for(int i=0;i<n;i++) {while(m>1&&Cross(ans[m-1]-ans[m-2],p[i]-ans[m-2])<=0)m--;ans[m++]=p[i];}int k=m;for(int i=n-2;i>=0;i--){while(m>k&&Cross(ans[m-1]-ans[m-2],p[i]-ans[m-2])<=0)m--;ans[m++]=p[i];}if(n>1)m--;return m;}int main(){while(cin>>n>>r){for(int i=0;i<n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);int m=Andrew();double dist=0.0;for(int i=0;i<m-1;i++)dist+=Len(ans[i],ans[i+1]);dist+=Len(ans[m-1],ans[0])+2*pi*r;printf("%d\n",(int)(dist+0.5));}return 0;}


原创粉丝点击