前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU 1392 Surround the Trees(计算几何--凸包)

HDU 1392 Surround the Trees(计算几何--凸包)

作者头像
用户2965768
发布2018-08-30 16:06:51
3620
发布2018-08-30 16:06:51
举报
文章被收录于专栏:wymwym

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1392

题意:计算凸包并求周长

题解;见代码

代码语言:javascript
复制
#include <iostream>
#include <algorithm>
#include <cmath>
#define ll long long 
using namespace std;
const ll inf=6666666;
struct NODE{
	int x,y;
};
NODE tr[105],l[105];
int t,top;
bool cmp(NODE a,NODE b)
{
		double A=atan2(a.y-tr[1].y,a.x-tr[1].x);
		double B=atan2(b.y-tr[1].y,b.x-tr[1].x);
		if(A!=B) return A<B;
		else return a.x<b.x;
}
int Cross(NODE a,NODE b,NODE c)
{
	return (a.x-b.x)*(b.y-c.y)-(a.y-b.y)*(b.x-c.x);
}
void Get()
{
	int k;
	tr[0]=NODE{inf,inf};
	for(int i=1;i<=t;i++)
	    if(tr[0].y>tr[i].y||(tr[0].y==tr[i].y&&tr[0].x>tr[i].x))
	    {
	    	tr[0]=tr[i];
	    	k=i;
		}
	swap(tr[k],tr[1]);
	sort(tr+2,tr+t+1,cmp);
	l[0]=tr[1]; l[1]=tr[2];  top=1;
	for(int i=3;i<=t;)
	{
		if(top&&(Cross(l[top-1],tr[i],l[top])>=0)) top--;
		else l[++top]=tr[i++];
	}
}	
inline double cal(NODE f,NODE g)
{
	return sqrt(1ll*(f.x-g.x)*(f.x-g.x)+1ll*(f.y-g.y)*(f.y-g.y));
}
inline void Getans()
{
	double ans=0;
	if(top==0)
	ans=0;
	else if(top==1)
	      ans+=cal(l[0],l[1]);
	      else {
	      	     l[++top]=l[0];
	      	     for(int i=0;i<top;i++)
	      	         {
	      	         	ans+=cal(l[i],l[i+1]);
					   }
		        }
		printf("%.2lf\n",ans);
} 
int main()
{
	while(scanf("%d",&t)&&t)
	{
		for(int i=1;i<=t;i++)
		{
			scanf("%d %d",&tr[i].x,&tr[i].y);
		}
		Get(); 
		Getans();
	}
	
  return 0;	
} 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年08月04日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档