hdu 1348 wall (计算几何,凸包)

题目链接:hdu 1348

凸包模板题。但是要注意输出格式,我开始就被输出格式给坑了。。。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn (1000+10)

int N,L;
double pi=acos(-1);

struct node{
	int x,y;
}p[maxn],point,stack[maxn];

inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

int get_cross(node a,node b,node c){
	return ((b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x));
}

double get_dis(node a,node b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

void find_point(){
	int col=0; node temp; point=p[0];
	for(int i=1;i<N;i++){
		if(p[i].y<point.y||(p[i].y==point.y&&p[i].x<point.x)){
			point=p[i]; col=i;
		}
	}
	temp=p[0]; p[0]=p[col]; p[col]=temp;
}

bool cmp(node a,node b){
	int K=get_cross(point,a,b);
	if(K<0)return true;
	if(K>0)return false;
	double xx=get_dis(point,a),yy=get_dis(point,b);
	return xx<yy;
}

void Graham(){
	int top=2; p[N]=p[0];
	stack[0]=p[0]; stack[1]=p[1]; stack[2]=p[2];
	for(int i=3;i<=N;i++){
		while(top>1&&get_cross(stack[top-1],stack[top],p[i])>=0)top--;
		stack[++top]=p[i];
	}
	double ans=0;
	node temp=stack[top],sai=temp; top--;
	while(top){
		node xx=stack[top]; top--;
		ans+=get_dis(xx,temp);
		temp=xx;
	}
	ans+=get_dis(sai,temp);
	ans+=pi*L*2;
	printf("%.0lf\n",ans);
}

int main(){
	int T=read();
	while(T--){
		N=read(); L=read();
		for(int i=0;i<N;i++){
			p[i].x=read(); p[i].y=read();
		}
		find_point();
		sort(p,p+N,cmp);
		Graham();
		if(T!=0)printf("\n");//注意未在最后一组数据时要输出换行符,否则会PE
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值