标签:point sort void art cstring define 技术 while 围墙
题目大意:建立围墙将城堡围起来,要求围墙至少距离城堡L,拐角处用圆弧取代,求围墙的长度。
题目思路:围墙长度=凸包周长+(2*PI*L),另外不知道为什么C++poj会RE,G++就没问题。
#include<cstdio> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<queue> #define INF 0x3f3f3f3f #define MAX 100005 #define PI acos(-1) using namespace std; struct node { int x,y; }point[MAX]; int Stuck[MAX],n,l,top; bool cmp(struct node A,struct node B) { if(A.y < B.y) return true; if(A.y==B.y && A.x<B.x) return true; return false; } int Cross(int x1,int y1,int x2,int y2,int x3,int y3) { return (x1-x2)*(y1-y3)-(x1-x3)*(y1-y2); } void Graham() { memset(Stuck,0,sizeof(Stuck)); Stuck[0]=0; Stuck[1]=1; top=1; for(int i=2;i<n;i++) { while(top>0 && Cross(point[Stuck[top]].x,point[Stuck[top]].y,point[Stuck[top-1]].x,point[Stuck[top-1]].y,point[i].x,point[i].y)<=0) top--; Stuck[++top]=i; } int len=top; Stuck[++top]=n-2; for(int i=n-3;i>=0;i--) { while(top!=len && Cross(point[Stuck[top]].x,point[Stuck[top]].y,point[Stuck[top-1]].x,point[Stuck[top-1]].y,point[i].x,point[i].y)<=0) top--; Stuck[++top]=i; } } double Dist(int pos1,int pos2) { return sqrt((point[pos1].x-point[pos2].x)*(point[pos1].x-point[pos2].x)*1.0+(point[pos1].y-point[pos2].y)*(point[pos1].y-point[pos2].y)); } int main() { int k; while(scanf("%d%d",&n,&l)!=EOF) { k=0; for(int i=0;i<n;i++) { scanf("%d%d",&point[i].x,&point[i].y); if(point[k].y > point[i].y || (point[k].x>point[i].x && point[k].y==point[i].y)) k=i; } swap(point[0],point[k]); sort(point+1,point+n,cmp); Graham(); double sum=0; sum+=Dist(Stuck[0],Stuck[top])+2*PI*l; for(int i=1;i<=top;i++) sum+=Dist(Stuck[i],Stuck[i-1]); int ans=(int)(sum+0.5); printf("%d\n",ans); } return 0; }
标签:point sort void art cstring define 技术 while 围墙
原文地址:http://www.cnblogs.com/alan-W/p/6030530.html