*HDU 1392 计算几何
Posted LuZhiyuan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了*HDU 1392 计算几何相关的知识,希望对你有一定的参考价值。
Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10403 Accepted Submission(s): 4033
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.
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.
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.
Sample Input
9
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0
Sample Output
243.06
Source
题意:
求以上n个点的凸包的周长
讲解很详细的博客:http://www.cnblogs.com/jbelial/archive/2011/08/05/2128625.html
代码:
//求凸包的模板题Graham扫描法。 //详解《算法导论》604页 //极角排序先比较象限再比较叉积。 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int INF=0x7fffffff; int top,n,q[109];//q用于保存组成凸包的点 struct Node { double x,y; }node[109]; double dis(Node p1,Node p2) { return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y)); } int Qd(Node p)//返回点相对于p[0]点所在的象限 { p.x-=node[0].x; p.y-=node[0].y; if(p.x>=0&&p.y>=0) return 1; else if(p.x<=0&&p.y>0) return 2; else if(p.x<0&&p.y<=0) return 3; else if(p.x>=0&&p.y<0) return 4; } double chaji(Node p0,Node p1,Node p2)//叉积 { return ((p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x)); } bool cmp(Node p1,Node p2) { int Q1=Qd(p1),Q2=Qd(p2); if(Q1==Q2){ double tmp=chaji(node[0],p1,p2); if(tmp>0) return 1;//tem>0说明向量p1p0在向量p2p0的顺时针方向即p1p0相对于p0的极角小于p2p0的 else if(tmp<0) return 0; else return fabs(p1.x)<fabs(p2.x); } else return Q1<Q2; } void tubao() { top=0; q[++top]=0; q[++top]=1; for(int i=2;i<=n;i++){ while(top>1&&chaji(node[q[top-1]],node[q[top]],node[i])<=0) top--; q[++top]=i; } } int main() { while(scanf("%d",&n)&&n){ double min_x=INF,min_y=INF; int min_i=0; for(int i=0;i<n;i++){ scanf("%lf%lf",&node[i].x,&node[i].y); if(min_y>node[i].y){ min_y=node[i].y; min_x=node[i].x; min_i=i; }else if(min_y==node[i].y&&min_x<node[i].x){ min_x=node[i].x; min_i=i; } } swap(node[min_i],node[0]); if(n==1) {printf("0.00\\n");continue;} if(n==2) {printf("%.2lf\\n",dis(node[0],node[1]));continue;}//计算凸包的点数必须多于2 sort(node+1,node+n,cmp); node[n].x=node[0].x;node[n].y=node[0].y;//形成闭合的凸包 tubao(); double ans=0; for(int i=1;i<top;i++){ ans+=dis(node[q[i]],node[q[i+1]]); } printf("%.2lf\\n",ans); } return 0; }
以上是关于*HDU 1392 计算几何的主要内容,如果未能解决你的问题,请参考以下文章
计算几何初步-凸包-Jarvis步进法。HDU1392Surround the Trees
HDU - 1392 Surround the Trees (凸包)