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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 活动图(Activity Diagram)

    活动图(Activity Diagram)一、什么是活动图?二、活动图的基本要素?三、活动图的作用?

    2022年6月6日
    223
  • java链表数据结构是什么_java 链表数据结构

    java链表数据结构是什么_java 链表数据结构首先,单链表相对于队列的优势在于存储地址不是连续的,这样的意义在于,操作其中的某一个位置的元素时不需要对之前的其他元素都进行内存操作,大大的为我们的计算机减压了。下面直接进入正题:先要定义一个结点类,如下:Java代码publicclassNode{Nodenext;//下一个结点的引用Objectobj;//结点元素publicNode(Objectobj){this.obj=obj;…

    2022年5月27日
    32
  • 华为数通hcie_通融理赔后需要签协议吗

    华为数通hcie_通融理赔后需要签协议吗Internet组管理协议称为IGMP协议(InternetGroupManagementProtocol),是因特网协议家族中的一个组播协议。该协议运行在主机和组播路由器之间。IGMP包含了IGMPv1、IGMPv2、IGMPv3三个版本,目前正处于由IGMPv2向IGMPv3的过渡阶段。本篇将按照IGMP基本原理、IGMP三个版本、IGMPSnooping几部分对IGMP协议进行介绍。

    2022年9月14日
    2
  • Pycharm 中导入模块

    Pycharm 中导入模块Pycharm中导入模块@TOC以PyQt5为例1、首先要在安装PyQt5。在windows中以管理员身份打开CRM,然后输入pipinstallpyqt5,安装好pyqt5后,还可以继续安装pyqt5的相关工具,命令pipinstallpyqt5-tools2、在Pycharm中安装pyqt5步骤:File-&gt;setting-&gt;project:***-&gt;p…

    2022年8月27日
    5
  • Django 安装_启动redis的命令

    Django 安装_启动redis的命令安装redis1.使用Homebrew安装Redisbrewinstallredis执行上述命令后出现以下内容,则成功安装Downloadfailed:https://mirrors.

    2022年7月31日
    8
  • 【机器学习】几种相似度算法分析

    最近开始研究推荐系统,其中常见的相似度算法有以下几种:1.欧几里得距离欧几里得度量(euclideanmetric)(也称欧氏距离)是一个通常采用的距离定义,指在m维空间中两个点之间的真实距离,或者向量的自然长度(即该点到原点的距离)。在二维和三维空间中的欧氏距离就是两点之间的实际距离。注意事项:a.因为计算是基于各维度特征的绝对数值,所以欧氏度量需要保证各维度指标在相同的刻度级别,比如对身高…

    2022年4月4日
    46

发表回复

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

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