*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.

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.

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

Asia 1997, Shanghai (Mainland China)

题意:

求以上n个点的凸包的周长

讲解很详细的博客:http://www.cnblogs.com/jbelial/archive/2011/08/05/2128625.html

代码:

  1 //求凸包的模板题Graham扫描法。
  2 //详解《算法导论》604页
  3 //极角排序先比较象限再比较叉积。
  4 #include<iostream>
  5 #include<cstdio>
  6 #include<cstring>
  7 #include<algorithm>
  8 #include<cmath>
  9 using namespace std;
 10 int top,n;
 11 struct nod
 12 {
 13     double x,y;
 14 } p[102],que[102];  //que用于保存组成凸包的点
 15 int xy(nod p0)   //返回点相对于p[0]点所在的象限
 16 {
 17     p0.x-=p[0].x;
 18     p0.y-=p[0].y;
 19     if(p0.x>=0&&p0.y>=0) return 1;
 20     if(p0.x<=0&&p0.y>0) return 2;
 21     if(p0.x<0&&p0.y<=0) return 3;
 22     if(p0.x>=0&&p0.y<0) return 4;
 23 }
 24 double dis(nod p1,nod p2)    //计算凸包边长
 25 {
 26     return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
 27 }
 28 double chaji(nod p0,nod p1,nod p2)  //叉积
 29 {
 30     return ((p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x));
 31 }
 32 bool cmp(nod p1,nod p2)
 33 {
 34     int L1=xy(p1),L2=xy(p2);
 35     if(L1==L2)
 36     {
 37         int tem=chaji(p[0],p1,p2);
 38         if(tem>0) return 1;   //tem>0说明向量p1p0在向量p2p0的顺时针方向即p1p0相对于p0的极角小于p2p0的
 39         if(tem<0) return 0;
 40         if(tem==0)
 41             return p1.x<p2.x;
 42     }
 43     else return L1<L2;
 44 }
 45 void tubao()
 46 {
 47     top=0;
 48     que[top].x=p[0].x;
 49     que[top++].y=p[0].y;
 50     que[top].x=p[1].x;
 51     que[top++].y=p[1].y;
 52     que[top].x=p[2].x;
 53     que[top].y=p[2].y;
 54     for(int i=3; i<=n; i++)
 55     {
 56         while(chaji(que[top-1],que[top],p[i])<=0)
 57             top--;
 58         que[++top].x=p[i].x;
 59         que[top].y=p[i].y;
 60     }
 61 }
 62 int main()
 63 {
 64     while(scanf("%d",&n)&&n)
 65     {
 66         int Miny=10000007,Minx=10000007,Mini;
 67         for(int i=0; i<n; i++)
 68         {
 69             scanf("%lf%lf",&p[i].x,&p[i].y);
 70             if(p[i].y<Miny)
 71             {
 72                 Miny=p[i].y;
 73                 Mini=i;
 74             }
 75             else if(p[i].y==Miny&&p[i].x<Minx)
 76             {
 77                 Minx=p[i].x;
 78                 Mini=i;
 79             }
 80         }
 81         if(n==1) {printf("0.00\n");continue;}
 82         if(n==2) {printf("%.2lf\n",dis(p[0],p[1]));continue;}  //计算凸包的点数必须多于2
 83         int tem=p[0].x;
 84         p[0].x=p[Mini].x;
 85         p[Mini].x=tem;
 86         tem=p[0].y;
 87         p[0].y=p[Mini].y;
 88         p[Mini].y=tem;
 89         sort(p+1,p+n,cmp);
 90         p[n].x=p[0].x;p[n].y=p[0].y; //形成闭合的凸包
 91         tubao();
 92         double ans=0.0;
 93         for(int i=0;i<top;i++)
 94         {
 95             ans+=dis(que[i],que[i+1]);
 96         }
 97         printf("%.2lf\n",ans);
 98     }
 99     return 0;
100 }
时间: 2024-08-01 10:45:01

*HDU 1392 计算几何的相关文章

hdu 1392 Surround the Trees (凸包)

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7043    Accepted Submission(s): 2688 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

HDU 1392 Surround the Trees

PS: 在求解两个点的时候就是两个点的距离,在这WA了一次. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; const int maxn = 110; struct point { int x, y; point(d

hdu 1392 Surround the Trees(凸包果题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7473    Accepted Submission(s): 2860 Problem Description There are a lot o

HDU - 1392 Surround the Trees (凸包)

Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出凸包上的点后,s数组中就是按顺序的点,累加一下距离就是周长了. #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdli

HDU 1392 Surround the Trees (凸包周长)

题目链接:HDU 1392 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

计算几何(凸包模板):HDU 1392

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

hdu 1616 计算几何 凸包

题意是一个世界有许多个国家,每个国家有N个建筑,包括一个发电站和N-1个用电建筑,所有建筑围成的凸包就是这个国家的面积.一枚导弹如果在一个国家之内爆炸则可以使这个国家停电. step 1:求出每个国家的凸包(我用水平排序就是各种坑,改叉乘排序才过,主要是后面求面积的时候需要这个叉乘排序的信息). step 2:判断每枚导弹是否在这个国家的范围之内. step 3:求出所有停电的国家的面积. 就是计算几何的综合模拟水题,坑点就是要小心(QAQ||写计算几何的题目都是要小心). 传送门:http:/

HDU 1392 Surround the Trees(几何 凸包模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1392 题目大意: 二维平面给定n个点,用一条最短的绳子将所有的点都围在里面,求绳子的长度. 解题思路: 凸包的模板.凸包有很多的算法.这里用Adrew. 注意这几组测试数据 1 1 1 3 0 0 1 0 2 0 输出数据 0.00 2.00 1 #include<cmath> 2 #include<cstdio> 3 #include<algorithm> 4 using name

hdu 3320 计算几何(三维图形几何变换)

openGL Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 170    Accepted Submission(s): 77 Problem Description Jiaoshou selected a course about “openGL” this semester. He was quite interested in m