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


相关推荐

  • 按位取反怎么运算_补码取反加一

    按位取反怎么运算_补码取反加一读本文前请首先搞懂“反码”,“取反”,“按位取反(~)”,这3个概念是不一样的。取反:0变1,1变0反码:正数的反码是其本身,对于负数其符号位不变其它各位取反(0变1,1变0)按位取反(~):这将是下面要讨论的。“~”运算符在c、c++、java、c#中都有,之前一直没有遇到这个运算符。要弄懂这个运算符的计算方法,首先必须明白二进制数在内存中的存放形式,二

    2022年8月15日
    8
  • Python 常用string函数

    Python 常用string函数字符串中字符大小写的变换1.str.lower()//小写>>>'SkatE'.lower()'skat

    2021年12月24日
    48
  • PyCharm找不到解释器no python interpreter configured[通俗易懂]

    PyCharm找不到解释器no python interpreter configured[通俗易懂]安装好PyCharm之后,新建或者导入项目碰到找不到解释器的情况,不用担心,追根到底,咱们就是需要找到pycharm*.exe的文件,那么这个文件在哪里呢?这是个问题。先打开File–>Setting–>Project,这时候看到选中栏显示的是Nointerpreter,在哪里找这个文件呢,不妨打开磁盘,直接搜索python.exe文件可能在C盘,也可能在其他磁盘,楼主找到的这…

    2022年8月28日
    3
  • 详解布隆过滤器原理,及分布式运用方法_布隆过滤器最小误差

    详解布隆过滤器原理,及分布式运用方法_布隆过滤器最小误差1.什么是布隆过滤器布隆过滤器是一个叫“布隆”的人提出的,本质上布隆过滤器是一种数据结构,比较巧妙的概率型数据结构(probabilisticdatastructure)。它本身是一个很长的二进制向量,特点是高效地插入和查询,可以用来确定“某一条数据一定不存在或者可能存在一个集合中”。相比于传统的List、Set、Map等数据结构,它更高效、占用空间更少(因为是个二进制的向量),但是缺点是其返回的结果是概率性的,而不是确切的。2.布隆过滤器数据结构布隆过滤器是一个bit向量或者

    2022年10月6日
    3
  • pycharm2021.11.3永久激活_最新在线免费激活

    (pycharm2021.11.3永久激活)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月28日
    58
  • 【优化算法】简述灰狼优化算法(GWO)原理[通俗易懂]

    【优化算法】简述灰狼优化算法(GWO)原理[通俗易懂]系列优化算法简述:OP_1.简述遗传算法(GA)原理OP_2简述灰狼优化算法(GWO)原理前言:灰狼优化算法(GreyWolfOptimizer,GWO)由澳大利亚格里菲斯大学学者Mirjalili等人于2014年提出来的一种群智能优化算法。该算法受到了灰狼捕食猎物活动的启发而开发的一种优化搜索方法,它具有较强的收敛性能、参数少、易实现等特点。近年来受到了学者的广泛关注…

    2022年10月19日
    2

发表回复

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

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