凸包方法解析
https://blog.csdn.net/gpc_123/article/details/122741381
Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7164 Accepted Submission(s): 2738
Problem Description
There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him?
The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.
There are no more than 100 trees.
Input
The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank.
Zero at line for number of trees terminates the input for your program.
Output
The minimal length of the rope. The precision should be 10^-2.
input :
9 12 7 24 9 30 5 41 9 80 7 50 87 22 9 45 1 50 7 0
output
243.06
输入n个点,求凸包的周长
#includeusing namespace std; typedef long long ll; const double pi = acos(-1.0);//高精度圆周率 const double eps = 1e-8;//偏差值 const int maxn = 104;//点的数量 //判断是否等于零,返回0为等于零,返回-1为小于,1为大于 int sgn(double x) { if (fabs(x) < eps)return 0; else return x < 0 ? -1 : 1; } struct Point { double x, y; Point() {} Point(double x, double y) : x(x), y(y) {} Point operator+(Point B) { return Point(x + B.x, y + B.y); } Point operator-(Point B) { return Point(x - B.x, y - B.y); } Point operator*(double k) { return Point(x * k, y * k); }//放大k倍 Point operator/(double k) { return Point(x / k, y / k); }//缩小k倍 bool operator==(Point B) { return sgn(x - B.x) == 0 && sgn(y - B.y) == 0; } // bool operator<(Point B) {return sgn(x - B.x) < 0 || (sgn(x - B.x) == 0 && sgn(y - B.y) < 0); } friend bool operator < (Point a,Point b){ return sgn(a.x - b.x) < 0 || (sgn(a.x - b.x) == 0 && sgn(a.y - b.y) < 0); } }; typedef Point Vector; double Distance(Point A, Point B) { return hypot(A.x - B.x, A.y - B.y);} double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y;} double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x;} int Convex_hull(Point *p,int n,Point *ch){ sort(p, p + n);//对点排序 n = unique(p, p + n) - p;//去重 int v = 0; //求下凸包,如果p[i]是右拐弯,这个点不在凸包上往回退 for (int i = 0; i < n;i ++){ while(v > 1 && sgn(Cross(ch[v - 1] - ch[v - 2],p[i] - ch[v - 2])) <= 0) v--; ch[v++] = p[i]; } int j = v; //求上凸包 for (int i = n - 2; i >= 0;i --){ while(v > j && sgn(Cross(ch[v - 1] - ch[v - 2],p[i] - ch[v - 2])) <= 0) v--; ch[v++] = p[i]; } if(n > 1) v--; return v; //返回凸包顶点数 } int main(){ int n; Point p[maxn], ch[maxn]; while(scanf("%d",&n) && n){ for (int i = 0; i < n;i ++) scanf("%lf%lf",&p[i].x,&p[i].y); int v = Convex_hull(p, n, ch); double ans = 0; if(v == 1) ans = 0; else if (v == 2) ans = Distance(ch[0], ch[1]); else{ for (int i = 0; i < v;i ++) ans += Distance(ch[i], ch[(i + 1) % v]); } printf("%.2fn", ans); } return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)