码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1113

时间:2014-07-31 12:55:46      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:blog   os   io   for   div   amp   log   ios   

求凸包,然后加上一个半径为L的圆周就好了。

因为每到一个凸包的顶点时,需要保持最小间距为L,则多个顶点围起来会有360度。

在输出时,不要把结果直接转化主INT,因为这个不是四舍五入。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
const int MAXN=1050;
int n,l;

int st[MAXN],stop,cnt;
int ans[MAXN];

struct point{
	int x,y;
}p[MAXN];

bool cmp(point A,point B){
	if(A.y<B.y) return true;
	else if(A.y==B.y){
		if(A.x<B.x) return true;
	}
	return false;
}

bool multi(point a,point b,point c){
	return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x)>0;
}

void slove(){
	stop=0; cnt=0;
	st[stop++]=0; st[stop++]=1;
	for(int i=2;i<n;i++){
		while(stop>1&&multi(p[st[stop-1]],p[i],p[st[stop-2]])) stop--;
		st[stop++]=i;
	}
	for(int i=0;i<stop;i++){
		ans[cnt++]=st[i];
	}
	stop=0;
	st[stop++]=n-1; st[stop++]=n-2;
	for(int i=n-3;i>=0;i--){
		while(stop>1&&multi(p[st[stop-1]],p[i],p[st[stop-2]])) stop--;
		st[stop++]=i;
	}
	for(int i=0;i<stop;i++)
	ans[cnt++]=st[i];
}

double dist(point a,point b){
	return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y)*1.0);
}

int main(){
	while(scanf("%d%d",&n,&l)!=EOF){
		for(int i=0;i<n;i++){
			scanf("%d%d",&p[i].x,&p[i].y);
		}
		sort(p,p+n,cmp);
		slove();
		double anl=4*acos(0.0)*l;
		for(int i=1;i<cnt;i++){
			anl+=dist(p[ans[i-1]],p[ans[i]]);
		}
		printf("%.0lf\n",anl);
	}
	return 0;
}

  

POJ 1113,布布扣,bubuko.com

POJ 1113

标签:blog   os   io   for   div   amp   log   ios   

原文地址:http://www.cnblogs.com/jie-dcai/p/3880392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!