hdu 1392 Surround the Trees

1 篇文章 0 订阅

简单凸包,叉乘的完美利用(右手定则)。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define maxn 110
using namespace std;
int n;
int top;
struct stu
{
	double x,y;
};
stu mapp[maxn],re[maxn];
stu s;
void input()//输入 
{
	for(int i=0;i<n;i++)
	{
		cin>>mapp[i].x>>mapp[i].y;
	}
}
double dis(stu x,stu y)//计算距离 
{
	return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}
double mul(stu x,stu y,stu z)
{
	return (y.x-x.x)*(z.y-x.y)-(y.y-x.y)*(z.x-x.x);
}
void find()//查找起始点 
{
	for(int i=0;i<n;i++)
	{
		if(i==0||mapp[i].y<s.y)
		{
			s=mapp[i];
			swap(mapp[0],mapp[i]);
		}
		else if(mapp[i].y==s.y)
		{
			if(mapp[i].x<s.x)
			{
				s=mapp[i];
				swap(mapp[0],mapp[i]);
			}
		}
	}
} 
bool cmp(stu x,stu y)
{
	double k=mul(s,x,y);
	if(k>0) return true;
	else if(k==0&&dis(s,x)<dis(s,y)) return true;
	return false;
}
void tubao()
{
	int i;
	for(i=0;i<3;i++) re[i]=mapp[i];
	top=2;
	for(i=3;i<=n;i++)
	{
		while(mul(re[top-1],re[top],mapp[i])<=0) top--;
		re[++top]=mapp[i];
	}
}
void cal()
{
	double len=0;
	for(int i=0;i<top;i++)
	{
		//cout<<re[i].x<<" "<<re[i].y<<"~"<<endl; 
		len+=dis(re[i],re[i+1]);
	}
	printf("%.2f\n",len);
	//cout<<top<<endl;
	//cout<<s.x<<" "<<s.y<<endl; 
}
int main()
{
	cin.sync_with_stdio(false);
	while(cin>>n&&n)
	{
		input();
		//特判 
		if(n==1)
		{
			cout<<"0.00"<<endl;
			continue;
		}
		else if(n==2)
		{
			printf("%.2f\n",dis(mapp[0],mapp[1]));
			continue;
		}
		find();//查找起始点 
		sort(mapp+1,mapp+n,cmp);//排序 
		/*for(int i=0;i<n;i++)
		{
			cout<<mapp[i].x<<" "<<mapp[i].y<<"~"<<endl;
		}*/
		mapp[n]=mapp[0];//成环 
		tubao();
		cal();
	}
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值