hdu_1392_Surround the Trees(凸包)

时间:2023-03-09 13:32:09
hdu_1392_Surround the Trees(凸包)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1392

题意:求凸包,不知道的百度

题解:模版题

 #include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
/*
* 求凸包,Graham算法 * 点的编号0~n-1
* 返回凸包结果Stack[0~top-1]为凸包的编号
*/
const int MAXN = ;
const double eps = 1e-;
const double PI = acos(-1.0);
int sgn(double x) {
if(fabs(x) < eps)return ;
if(x < )return -;
else return ; }
struct Point {
double x,y;
Point(){}
Point(double _x,double _y){x = _x,y = _y;}
Point operator -(const Point &b)const{return Point(x-b.x,y-b.y);}//叉积
double operator ^(const Point &b)const{return x*b.y-y*b.x;}//点积
double operator *(const Point &b)const{return x*b.x + y*b.y;}//绕原点旋转角度B(弧度值),后x,y的变化
void transXY(double B){double tx = x,ty = y,x = tx*cos(B) - ty*sin(B),y = tx*sin(B) + ty*cos(B);}
}list[MAXN];
int S[MAXN],top;//相对于list[0]的极角排序 double dist(Point a,Point b){return sqrt((a-b)*(a-b));}
bool _cmp(Point p1,Point p2){
double tmp =(p1-list[])^(p2-list[]);
if(sgn(tmp)>)return ;
else if(sgn(tmp)==&&sgn(dist(p1,list[])-dist(p2,list[]))<= )return ;
return ;
}
void Graham(int n){
Point p0=list[],tp;
int k=;
for(int i=;i<n;i++)if((p0.y > list[i].y)||(p0.y ==list[i].y&&p0.x>list[i].x))p0 =list[i],k=i;
tp=list[k],list[k]=list[],list[]=tp,sort(list+,list+n,_cmp);
if(n==){top=,S[]=;return;}
if(n==){top=,S[]=,S[]=;return;}
S[]=,S[]=,top=;
for(int i=;i<n;i++){
while(top>&&sgn((list[S[top-]]-list[S[top-]])^(list[i]-list[S[top-]]))<=)top--;
S[top++]=i;
}
} int main(){
int n;
while(~scanf("%d",&n),n){
for(int i=;i<n;i++)scanf("%lf%lf",&list[i].x,&list[i].y);
if(n==){puts("0.00");continue;}
else if(n==){printf("%.2lf\n",dist(list[],list[]));continue;}
Graham(n);
double ans=;
for(int i=;i<top-;i++)ans+=dist(list[S[i]],list[S[i+]]);
ans+=dist(list[S[]],list[S[top-]]);
printf("%.2lf\n",ans);
}
return ;
}