POJ-1113-Wall(凸包)

鏈接:http://poj.org/problem?id=1113


大致題意:N個點圍成的城堡,求距離城堡大於L處建圍牆,求圍牆的最短距離

凸包問題,考慮到對於一個凸包上的一個x度的角,其需要一個180-x度半徑爲L的圓弧形狀圍牆,即求凸包周長+以L爲半徑圓的周長。

//FS
//#pragma comment(linker, "/stack:1024000000,1024000000")  
//#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;  
#define INF 0x3f3f3f3f  
#define MAXM 100005  
const double eps = 1e-8;
const double PI = acos(-1.0);
const long long mod=1e9+7;
const int MAXN = 2007;
int sgn(double x)
{
	if(fabs(x)<eps)return 0;
	if(x<0)return -1;
	return 1;
}
struct Point
{
	double x,y;
	Point(){}
	Point(double _x, double _y){x=_x,y=_y;}
	bool operator <(Point b)const
	{
		return sgn(x-b.x)==0? sgn(y-b.y)<0:x<b.x;
	}
	Point operator -(const Point &b)const
	{
		return Point(x-b.x,y-b.y);
	}
	double operator ^(const Point &b)const //叉積
	{
		return x*b.y-b.x*y;
	}
	double dist(Point p)
	{
		return hypot(x-p.x,y-p.y);
	}
	double len()
	{
		return hypot(x,y);
	}
};
Point List[MAXN];
int Stack[MAXN],top;
bool cmp(Point p1, Point p2)
{
	double temp = (p1-List[0])^(p2-List[0]);
	if(sgn(temp)>0)return 1;
	if(sgn(temp)==0 && List[0].dist(p1)<=List[0].dist(p2))return 1;
	return 0;
}
void Graham(int n)
{
	Point p;
	int k=0;
	p=List[0];
	for(int i=1; i<n; ++i)
		if( (p.y>List[i].y) || (p.y==List[i].y && p.x>List[i].x))
		{
			p=List[i];
			k=i;
		}
	swap(List[k],List[0]);
	sort(List+1,List+n,cmp);
	Stack[0]=0;
	top=1;
	if(n==1)return;
	Stack[1]=1;
	top=2;
	if(n==2)return ;
	for(int i=2; i<n; ++i)
	{
		while(top>1 && sgn( (List[ Stack[top-1] ]-List[ Stack[top-2] ])^(List[i]-List[ Stack[top-2] ]))<=0)
			top--;
		Stack[top++]=i;
	}
}
int main()  
{
    int n,l;
    while(scanf("%d%d",&n,&l)!=EOF)
    {
    	for(int i=0; i<n; ++i)
    		scanf("%lf%lf",&List[i].x,&List[i].y);
    	Graham(n);
    	double ans=0;
    	for(int i=1; i<top; ++i)
    		ans+=List[Stack[i]].dist( List[Stack[i-1]] );
    	ans+=List[Stack[0]].dist(List[Stack[top-1]]);
    	ans+=2.0*PI*l;
    	printf("%d\n", (int)(ans+0.5));
    }
    return 0;
}  
/*
9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
*/



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章