hdu 1392 Surround the Trees 基础 二维凸包 算法
来源:互联网 发布:java中的流是什么意思 编辑:程序博客网 时间:2024/05/18 03:53
Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4726 Accepted Submission(s): 1784
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
#include<stdio.h>#include<math.h>#include<cstdlib>const int MAXN=109;const double eps=1e-6;struct point{ double x,y;}p[MAXN],h[MAXN];//p数组用于存所有点,h数组用于存凸包点double distance(point p1,point p2){ return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); }// 求两点之间的距离double multiply(point sp,point ep,point op){ return ((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y)); }//判断sp,ep,op是否满足左转,op是向量起始点,sp与op是两向量另外的端点int cmp(const void *a,const void *b){//按极角排序 point *p1=(point *)a; point *p2=(point *)b; double t=multiply(*p2,*p1,p[0]); if(t>eps)return 1; else if(fabs(t)<=eps) {if(distance(p[0],*p1)>distance(p[0],*p2))return 1;elsereturn -1; } elsereturn -1;}void anglesort(point p[],int n){//找到最左下方的点 int i,k=0; point temp; for(i=1;i<n;i++) if(p[i].x<p[k].x||((p[i].x==p[k].x)&&(p[i].y<p[k].y))) k=i; temp=p[0],p[0]=p[k],p[k]=temp; qsort(p+1,n-1,sizeof(point),cmp);//找到左下点后再将左下点作为中心进行极角排序,从第p[1]个元素开始,共n-1个元素}void Graham_scan(point p[],point ch[],int n,int &len){//建立凸包 int i,top=2; anglesort(p,n); if(n<3){ for(i=0,len=n;i<n;i++)ch[i]=p[i]; return;//终止函数 } ch[0]=p[0],ch[1]=p[1],ch[2]=p[2]; p[n]=p[0]; for(i=3;i<=n;i++){ while(multiply(ch[top],p[i],ch[top-1])<=0)top--; ch[++top]=p[i]; } len=top+1;}int main(){ int i,n,len; while(scanf("%d",&n)!=EOF&&n){ for(i=0;i<n;i++)scanf("%lf %lf",&p[i].x,&p[i].y); Graham_scan(p,h,n,len);//p为所有的点h为凸包点len为凸包点的个数 double sum=0; for(i=1;i<len;i++) { sum+=distance(h[i],h[i-1]); } printf("%.2lf\n",sum);} return 0; }
- hdu 1392 Surround the Trees 基础 二维凸包 算法
- HDU 1392 Surround the Trees(凸包)
- hdu-1392 Surround the Trees(凸包)
- HDU 1392 Surround the Trees(凸包)
- hdu 1392 Surround the Trees 凸包
- hdu 1392 Surround the Trees【凸包】
- hdu 1392 Surround the Trees(凸包模板)
- ZJU 1453(hdu 1392) Surround the Trees(凸包)
- hdu 1392 Surround the Trees(凸包模版)
- hdu 1392 Surround the Trees 凸包模板题
- HDU 1392 Surround the Trees 构造凸包
- HDU 1392 Surround the Trees【凸包周长】
- hdu 1392 Surround the Trees(凸包模板题)
- hdu 1392 Surround the Trees(凸包)
- HDU-#1392 Surround the Trees(Graham & Andrew +凸包)
- HDU 1392 Surround the Trees 凸包的周长
- HDU 1392 Surround the Trees(凸包周长)
- hdu 1392 Surround the Trees( 凸包问题)
- html5游戏开发-愤怒的小鸟-开源讲座(一)-跳入弹出的小鸟
- struct timeval结构体
- WCF Test Client 中如何为添加array 类型参数
- 尚学堂马士兵Linux视频教程笔记
- liunx 下部分压缩解压命令介绍
- hdu 1392 Surround the Trees 基础 二维凸包 算法
- 黑马程序员-访问修饰符的学习
- object-c 学习第二天(类对象和方法)
- [转]研究发现:乳酸菌可改善睡眠障碍
- 云计算等入选10大赚钱行业,你“动心”了吗?
- 通过IP查询服务器的操作系统类型
- Android动画开发——Animation动画效果
- JdbcTemplate 执行批量操作
- 外贸零售业风险分析——如何正确使用PayPal