顺便整理出来一份自己看着比较顺眼的模板
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 1010 12 #define LL long long 13 #define INF 0xfffffff 14 const double eps = 1e-8; 15 const double pi = 3.141592653; 16 const double inf = ~0u>>2; 17 struct Point 18 { 19 double x,y; 20 Point(double x=0,double y=0):x(x),y(y) {} //构造函数 方便代码编写 21 }p[N],ch[N]; 22 typedef Point pointt; 23 pointt operator + (Point a,Point b) 24 { 25 return Point(a.x+b.x,a.y+b.y); 26 } 27 pointt operator - (Point a,Point b) 28 { 29 return Point(a.x-b.x,a.y-b.y); 30 } 31 pointt operator * (Point a,double b) 32 { 33 return Point(a.x*b,a.y*b); 34 } 35 pointt operator / (Point a,double b) 36 { 37 return Point(a.x/b,a.y/b); 38 } 39 bool operator < (const Point &a,const Point &b) 40 { 41 return a.x<b.x||(a.x==b.x&&a.y<b.y); 42 } 43 int dcmp(double x) 44 { 45 if(fabs(x)<eps) return 0; 46 else return x<0?-1:1; 47 } 48 bool operator == (const Point &a,const Point &b) 49 { 50 return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0; 51 } 52 //求点积以及利用点积求长度和夹角的函数 53 double dot(Point a,Point b) 54 { 55 return a.x*b.x+a.y*b.y; 56 } 57 //叉积及叉积求面积 58 double cross(Point a,Point b) 59 { 60 return a.x*b.y-a.y*b.x; 61 } 62 double dis(Point a) 63 { 64 return sqrt(dot(a,a)); 65 } 66 double mul(Point p0,Point p1,Point p2) 67 { 68 return cross(p1-p0,p2-p0); 69 } 70 bool cmp(Point a,Point b) 71 { 72 if(dcmp(mul(p[1],a,b))==0) 73 return dis(a-p[1])<dis(b-p[1]); 74 else 75 return dcmp(mul(p[1],a,b))>0; 76 } 77 int Graham(int n) 78 { 79 int i,k = 1,top = 1; 80 Point tmp; 81 if(n<=3) 82 { 83 for(i = 1; i <= 3 ; i++) 84 ch[i] = p[i]; 85 return n+1; 86 } 87 for(i = 1 ; i <= n; i++) 88 { 89 if(p[i].y<p[k].y||(p[i].y==p[k].y&&p[i].x<p[k].x)) 90 k = i; 91 } 92 if(k!=1) 93 { 94 tmp = p[1]; p[1] = p[k]; p[k] = tmp; 95 } 96 sort(p+2,p+n+1,cmp); 97 ch[top++] = p[1]; 98 ch[top++] = p[2]; 99 ch[top++] = p[3]; 100 for(i = 4; i <= n ;) 101 { 102 if(top<3||mul(ch[top-2],ch[top-1],p[i])>0) 103 { 104 ch[top++] = p[i++]; 105 } 106 else top--; 107 } 108 return top; 109 } 110 int main() 111 { 112 int i,n,r; 113 while(scanf("%d%d",&n,&r)!=EOF) 114 { 115 for(i = 1 ; i <= n ; i++) 116 { 117 scanf("%lf%lf",&p[i].x,&p[i].y); 118 } 119 int top = Graham(n); 120 ch[top] = p[1]; 121 double ma; 122 ma = 2*pi*r; 123 for(i = 1 ; i < top ; i++) 124 { 125 ma += dis(ch[i]-ch[i+1]); 126 } 127 printf("%.0f\n",ma); 128 } 129 return 0; 130 }
poj1113Wall(凸包)
时间: 2024-10-20 23:43:44