POJ1113 Wall 凸包
Posted 声声醉如兰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ1113 Wall 凸包相关的知识,希望对你有一定的参考价值。
题目大意:建立围墙将城堡围起来,要求围墙至少距离城堡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; }
以上是关于POJ1113 Wall 凸包的主要内容,如果未能解决你的问题,请参考以下文章