ACM-计算几何之Surround the Trees——hdu1392
2014-04-26 17:30
281 查看
Surround the Trees
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1392
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6916 Accepted Submission(s): 2643
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
计算几何中,凸包应用题目,求凸包的周长。
恩,就是求出凸包,然后再算周长即可。
凸包的求法有很多种,这里用的是Graham算法。
此算法具体将专门整理一篇文章来说。。。
这道题,主要注意一下n=0,1,2的情况。
/*
Author:Tree
From: http://blog.csdn.net/lttree Surround the Trees
hdu 1392
凸包周长
*/
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
struct point
{
double x,y;
}pnt[10001],res[10001];
// 两向量叉积
double cross( point sp,point ep,point op)
{
return (sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y);
}
// sort数组排序,比较的<号重载
bool operator < (const point &l,const point &r)
{
return l.y<r.y || (l.y==r.y && l.x<r.x) ;
}
// 求两点间几何距离
double dis(point a,point b)
{
return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
}
int Graham( int n )
{
int i,len,top=1;
// 先对所有点按照极角排序
// 纵坐标最小的排在前面,纵坐标相同,横坐标小的排在前面
sort(pnt,pnt+n);
// 判断点的个数是否大于2(所给的点能否构成凸包)
if( n==0 ) return 0;res[0] = pnt[0];
if( n==1 ) return 1;res[1] = pnt[1];
if( n==2 ) return 2;res[2] = pnt[2];
// 用叉积来判断后面的点是否为凸包中的点
for( i=2;i<n;++i )
{
while( top && cross(pnt[i],res[top],res[top-1])>=0 ) top--;
res[++top] = pnt[i];
}
len = top; res[++top] = pnt[n-2];
// 判断最后三个点
for(i=n-3;i>=0;--i)
{
while( top!=len && cross(pnt[i],res[top],res[top-1])>=0 ) top--;
res[++top] = pnt[i];
}
return top;
}
int main()
{
int n,i,len;
double sum;
while(scanf("%d",&n)!=EOF && n)
{
for(i=0;i<n;++i)
scanf("%lf%lf",&pnt[i].x,&pnt[i].y);
// len为凸包数组内点的个数
len=Graham(n);
// 判断n为0,1,2的情况(无法构造凸包的情况)
if(len==0 || len==1) {printf("0\n");continue;}
if(len==2) {printf("%.2lf\n",dis(res[0],res[1]));continue;}
// 求周长
sum=0;
for(i=0;i<len;++i)
sum=sum+dis(res[i],res[i+1]);
printf("%.2lf\n",sum);
}
return 0;
}
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1392
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6916 Accepted Submission(s): 2643
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
计算几何中,凸包应用题目,求凸包的周长。
恩,就是求出凸包,然后再算周长即可。
凸包的求法有很多种,这里用的是Graham算法。
此算法具体将专门整理一篇文章来说。。。
这道题,主要注意一下n=0,1,2的情况。
/*
Author:Tree
From: http://blog.csdn.net/lttree Surround the Trees
hdu 1392
凸包周长
*/
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
struct point
{
double x,y;
}pnt[10001],res[10001];
// 两向量叉积
double cross( point sp,point ep,point op)
{
return (sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y);
}
// sort数组排序,比较的<号重载
bool operator < (const point &l,const point &r)
{
return l.y<r.y || (l.y==r.y && l.x<r.x) ;
}
// 求两点间几何距离
double dis(point a,point b)
{
return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
}
int Graham( int n )
{
int i,len,top=1;
// 先对所有点按照极角排序
// 纵坐标最小的排在前面,纵坐标相同,横坐标小的排在前面
sort(pnt,pnt+n);
// 判断点的个数是否大于2(所给的点能否构成凸包)
if( n==0 ) return 0;res[0] = pnt[0];
if( n==1 ) return 1;res[1] = pnt[1];
if( n==2 ) return 2;res[2] = pnt[2];
// 用叉积来判断后面的点是否为凸包中的点
for( i=2;i<n;++i )
{
while( top && cross(pnt[i],res[top],res[top-1])>=0 ) top--;
res[++top] = pnt[i];
}
len = top; res[++top] = pnt[n-2];
// 判断最后三个点
for(i=n-3;i>=0;--i)
{
while( top!=len && cross(pnt[i],res[top],res[top-1])>=0 ) top--;
res[++top] = pnt[i];
}
return top;
}
int main()
{
int n,i,len;
double sum;
while(scanf("%d",&n)!=EOF && n)
{
for(i=0;i<n;++i)
scanf("%lf%lf",&pnt[i].x,&pnt[i].y);
// len为凸包数组内点的个数
len=Graham(n);
// 判断n为0,1,2的情况(无法构造凸包的情况)
if(len==0 || len==1) {printf("0\n");continue;}
if(len==2) {printf("%.2lf\n",dis(res[0],res[1]));continue;}
// 求周长
sum=0;
for(i=0;i<len;++i)
sum=sum+dis(res[i],res[i+1]);
printf("%.2lf\n",sum);
}
return 0;
}
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- ACM posters
- zoj3549 快速幂
- 浅谈manacher算法 最长回文子串(Longest Palindromic Substring)
- 前缀表达式,中缀表达式,后缀表达式转化和计算
- c/c++中让输入以回车换行键结束输入
- 图论学习笔记之一——Floyd算法
- 用单调栈解决最大连续矩形面积问题
- NWERC2010 NKOJ2178 Stock Prices
- 2011ACM福州网络预选赛B题 HDU4062 Abalone
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)