*HDU 1392 计算几何
2016-11-19 14:05
197 查看
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
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
The minimal length of the rope. The precision should be 10^-2.
[align=left]Sample Input[/align]
9
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0
[align=left]Sample Output[/align]
243.06
[align=left]Source[/align]
Asia 1997, Shanghai (Mainland China)
题意:
求以上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 .x=node[0].x;node .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 Surround the Trees(计算几何,求凸包周长)
- hdu 1392:Surround the Trees(计算几何,求凸包周长)
- 【集训笔记】计算几何【HDOJ2036【HDOJ1086【HDOJ1115【HDOJ1147【HDOJ1392 【ZOJ2976
- HDU 1392 (计算几何 凸包)
- *HDU 1007 计算几何
- SPOJ CIRU The area of the union of circles (计算几何)
- poj 1654 Area ----- 计算几何
- POJ 1556 The Doors 计算几何+ 最短路
- POJ 2405 Beavergnaw (计算几何-简单的问题)
- Codeforces 1C. Ancient Berland Circus(计算几何:正多边形性质+高精度)
- usc week 5 计算几何
- HDU 5572 An Easy Physics Problem (计算几何 点类 线类 向量类 线段与圆相交)
- 计算几何模板啦啦啦啦
- 计算几何专项:UVa 11437
- hdu 1756 Cupid's Arrow(计算几何)
- 计算几何的另外一下经典算法
- 1213: [视频]【计算几何】面积
- 汇总从代数角度与几何角度理解相似度计算方法(以机器学习Mahout框架为主线)
- poj 2606_Rabbit hunt_计算几何
- hdu1115(计算多边形几何重心)