二維凸包的板子
阿新 • • 發佈:2018-12-27
二維凸包的板子
感覺沒什麼可說的,碼風抄的孔老師的。
#include <cstdio> #include <algorithm> #include <cmath> const int N=1e4+10; #define Point Vector const double eps=1e-8; bool cmp(double a,double b){return fabs(a-b)<eps;} struct Vector { double x,y; Vector(){} Vector(double x,double y){this->x=x,this->y=y;} bool friend operator <(Vector n1,Vector n2){return cmp(n1.x,n2.x)?n1.y<n2.y:n1.x<n2.x;} Vector friend operator -(Vector n1,Vector n2){return Vector(n1.x-n2.x,n1.y-n2.y);} }pot[N],s[N]; int n,tot; double Cross(Point n1,Point n2){return n1.x*n2.y-n1.y*n2.x;} double Dis(Point n1,Point n2){return sqrt((n1.x-n2.x)*(n1.x-n2.x)+(n1.y-n2.y)*(n1.y-n2.y));} void push(Point n) { while(tot>1&&Cross(s[tot]-s[tot-1],n-s[tot])<0) --tot; s[++tot]=n; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lf%lf",&pot[i].x,&pot[i].y); std::sort(pot+1,pot+1+n); s[++tot]=pot[1]; for(int i=2;i<=n;i++) push(pot[i]); for(int i=n-1;i;i--) push(pot[i]); double ans=Dis(s[1],s[tot]); for(int i=2;i<=tot;i++) ans+=Dis(s[i],s[i-1]); printf("%.2lf\n",ans); return 0; }
2018.12.27