HDUJ 1392 Surround the Trees 凸包

HDUJ 1392 Surround the Trees 凸包

大家好,又见面了,我是全栈君。

Surround the Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7203    Accepted Submission(s): 2752

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.


HDUJ 1392 Surround the Trees 凸包

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

水平序的Andrew算法:

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

struct node
{
	double x,y;
}a[105],b[105];

double cmp(node n,node m)    //先比較X坐标,在比較Y坐标(从小到大)
{
	if(n.x != m.x)   
		return  n.x < m.x;
	else
		return  n.y < m.y;
}

double Cross(node a,node b,node c)     //计算叉积大小
{
	return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}

double dis(node a,node b)     //计算距离
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

int CH(node* a,int n,node* b)
{
	sort(a,a+n,cmp);
	int m=0,i;
	for(i=0;i<n;i++)    //从左往右,选下边界
	{
		while(m > 1 && Cross(b[m-2],b[m-1],a[i]) < 0)
			m--;
		b[m++]=a[i];
	}


	int k=m;
	for(i=n-2;i>=0;i--)    //从右往左,选上边界
	{
		while(m > k && Cross(b[m-2],b[m-1],a[i]) < 0)
			m--;
		b[m++]=a[i];
	}
	
	if(n >1)  m--;
	return m;
}

int main()
{
	int n;
	while(cin>>n)
	{
		if(n==0)   break;
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));

		int i,j;
		for(i=0;i<n;i++)
		{
			cin>>a[i].x>>a[i].y;
		}
		
	//	cout<<CH(a,n,b)<<endl;     //输出所选点的总数
		if(n==1)   
			cout<<0.00<<endl;
		else  if(n==2)
			printf("%.2lf\n",dis(a[0],a[1]));	
		else
		{
			int m=CH(a,n,b);
			double s=0;
			for(i=1;i<m;i++)
				s+=dis(b[i-1],b[i]);
			s+=dis(b[0],b[m-1]);
			printf("%.2lf\n",s);
		}
	//	for(i=0;i<CH(a,n,b);i++)    //输出所选点的坐标
	//		cout<<b[i].x<<" "<<b[i].y<<endl;   

	}

	return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115357.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 云计算平台构建与实验设计

    课程设计任务书一、作业目的  物联网的核心是应用,应用的核心是云计算。通过构建一个云计算平台,并利用这个平台设计云计算实验,将结果与普通的电脑计算比较两者的差别,感受云计算的优越性能,从而对物联网有更深刻的体验与认识。二、作业内容及要求    能够按照课程设计任务书按照相应的要求完成整个云计算平台的搭建,要完成这个任务,就要对云计算平台的架构和原理有一定的认识,对物联网的应用层有深入的学习,通…

    2022年4月6日
  • 用计算机算组合数_计算组合数

    用计算机算组合数_计算组合数计算组合数最大的困难在于数据的溢出,对于大于150的整数n求阶乘很容易超出double类型的范围,那么当C(n,m)中的n=200时,直接用组合公式计算基本就无望了。另外一个难点就是效率。对于第一个

    2022年8月6日
  • ubuntu下deb包安装方法_ubuntu安装下载文件

    ubuntu下deb包安装方法_ubuntu安装下载文件deb包是Debian,Ubuntu等Linux发行版的软件安装包,扩展名为.deb,是类似于rpm的软件包,Debian,Ubuntu系统不推荐使用deb软件包,因为要解决软件包依赖问题,安装也比较麻烦。如果Ubuntu要安装新软件,已有deb安装包(例如:linuxidc.deb),但是无法登录到桌面环境。那该怎么安装?答案是:使用dpkg命令。dpkg是Debianlinuxidc的简写,是为Debian专门开发的套件管理系统,方便软件的安装、更新及移除。所有源自Debian的Linux发行

    2022年10月20日
  • idea配置Tomcat_tomcat docbase

    idea配置Tomcat_tomcat docbaseidea配置tomcaterror:duplicatecontextpath 解决方法:    在editconfig中部署tomcat,     server         tomcathome            D:\software……\apache-tomcat-7.0.65   …

    2022年9月17日
  • Python requests post 请求报错:415 Unsupported Media Type

    Python requests post 请求报错:415 Unsupported Media Type本文仅供学习交流使用,如侵立删!联系方式及demo下载见文末requestspost请求报错:415UnsupportedMediaType在使用response.post发送json数据时,出现如题所示错误,是因为User-Agent被服务器设置拒绝请求了解决方法:’content-type’:’application/json’headers={‘User-Agent’:’Dalvik/2.1.0(Linux;U;Android6.0.1;Nexus5

    2022年6月12日
  • matlab高斯型隶属函数,图高斯型隶属函数.PPT[通俗易懂]

    matlab高斯型隶属函数,图高斯型隶属函数.PPT[通俗易懂]图高斯型隶属函数第三章模糊控制的理论基础第一节概述第二节模糊集合第三节隶属函数第四节模糊关系第五节模糊推理在模糊控制中应用较多的隶属函数有以下6种隶属函数。(1)高斯型隶属函数高斯型隶属函数由两个参数和c确定:其中参数b通常为正,参数c用于确定曲线的中心。Matlab表示为(2)广义钟型隶属函数广义钟型隶属函数由…

    2022年10月25日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号