hdu_1392_Surround the Trees(凸包)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1392
题意:求凸包,不知道的百度
题解:模版题
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 using namespace std; 5 /* 6 * 求凸包,Graham算法 * 点的编号0~n-1 7 * 返回凸包结果Stack[0~top-1]为凸包的编号 8 */ 9 const int MAXN = 110; 10 const double eps = 1e-8; 11 const double PI = acos(-1.0); 12 int sgn(double x) { 13 if(fabs(x) < eps)return 0; 14 if(x < 0)return -1; 15 else return 1; } 16 struct Point { 17 double x,y; 18 Point(){} 19 Point(double _x,double _y){x = _x,y = _y;} 20 Point operator -(const Point &b)const{return Point(x-b.x,y-b.y);}//叉积 21 double operator ^(const Point &b)const{return x*b.y-y*b.x;}//点积 22 double operator *(const Point &b)const{return x*b.x + y*b.y;}//绕原点旋转角度B(弧度值),后x,y的变化 23 void transXY(double B){double tx = x,ty = y,x = tx*cos(B) - ty*sin(B),y = tx*sin(B) + ty*cos(B);} 24 }list[MAXN]; 25 int S[MAXN],top;//相对于list[0]的极角排序 26 27 double dist(Point a,Point b){return sqrt((a-b)*(a-b));} 28 bool _cmp(Point p1,Point p2){ 29 double tmp =(p1-list[0])^(p2-list[0]); 30 if(sgn(tmp)>0)return 1; 31 else if(sgn(tmp)==0&&sgn(dist(p1,list[0])-dist(p2,list[0]))<= 0)return 1; 32 return 0; 33 } 34 void Graham(int n){ 35 Point p0=list[0],tp; 36 int k=0; 37 for(int i=1;i<n;i++)if((p0.y > list[i].y)||(p0.y ==list[i].y&&p0.x>list[i].x))p0 =list[i],k=i; 38 tp=list[k],list[k]=list[0],list[0]=tp,sort(list+1,list+n,_cmp); 39 if(n==1){top=1,S[0]=0;return;} 40 if(n==2){top=2,S[0]=0,S[1]=1;return;} 41 S[0]=0,S[1]=1,top=2; 42 for(int i=2;i<n;i++){ 43 while(top>1&&sgn((list[S[top-1]]-list[S[top-2]])^(list[i]-list[S[top-2]]))<=0)top--; 44 S[top++]=i; 45 } 46 } 47 48 int main(){ 49 int n; 50 while(~scanf("%d",&n),n){ 51 for(int i=0;i<n;i++)scanf("%lf%lf",&list[i].x,&list[i].y); 52 if(n==1){puts("0.00");continue;} 53 else if(n==2){printf("%.2lf\n",dist(list[0],list[1]));continue;} 54 Graham(n); 55 double ans=0; 56 for(int i=0;i<top-1;i++)ans+=dist(list[S[i]],list[S[i+1]]); 57 ans+=dist(list[S[0]],list[S[top-1]]); 58 printf("%.2lf\n",ans); 59 } 60 return 0; 61 }