您的位置:首页 > 其它

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

2015-03-15 21:26 405 查看
题目链接: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
=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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: