poj1113

解題思路:

         首先明確這道題是求凸包周長加上一個圓周長,即包圍凸包的一個圓角多邊形。因爲每個圓角是以凸包對應的頂點爲圓心,給定的L爲半徑,與相鄰兩條邊的切點之間的一段圓弧。每個圓弧的兩條半徑夾角與對應的凸包的內角互補。假設凸包有n條邊,則所有圓弧角之和爲180°*n-180°*n-2=360°。故圍牆周長爲=n條平行於凸包的線段+n條圓弧的長度=凸包周長+圍牆離城堡距離L爲半徑的圓周長。

 

拿小媛模板做的。

#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
using namespace std;
const int MAX = 120000;
const double eps = 1e-6;
const double pi=3.141592654;
bool dy(double x,double y)	{	return x > y + eps;}	// x > y 
bool xy(double x,double y)	{	return x < y - eps;}	// x < y 
bool dyd(double x,double y)	{ 	return x > y - eps;}	// x >= y 
bool xyd(double x,double y)	{	return x < y + eps;} 	// x <= y 
bool dd(double x,double y) 	{	return fabs( x - y ) < eps;}  // x == y
struct point{	double x,y;		};
point c[MAX];
double disp2p(point a,point b) 
{
	return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
}
double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向 
{
	return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
bool cmp(point a,point b)  // 排序   
{  
    double len = crossProduct(c[0],a,b);  
    if( dd(len,0.0) )  
        return xy(disp2p(c[0],a),disp2p(c[0],b));  
    return xy(len,0.0);  
}  
int stk[MAX];
int top;	
double sum = 0.0;
void Graham(int n)
{
    int tmp = 0;  
    for(int i=1; i<n; i++)//
    	if( xy(c[i].x,c[tmp].x) || dd(c[i].x,c[tmp].x) && xy(c[i].y,c[tmp].y) )
    		tmp = i;
    swap(c[0],c[tmp]);
    sort(c+1,c+n,cmp);
    stk[0] = 0; stk[1] = 1;
    top = 1;
	for(int i=2; i<n; i++)
	{
		while( xyd( crossProduct(c[stk[top]],c[stk[top-1]],c[i]), 0.0 ) && top >= 1 )
			top--;
		stk[++top] = i;
	}
	//cout<<"top "<<top<<endl;	
}
int main()
{
	int n,l;
	while(cin>>n>>l && n )
	{
		  for(int i=0;i<n;i++)
		  cin>>c[i].x>>c[i].y;
          Graham(n);
          double sum=2*pi*l;
        for(int i=0; i<=top; i++)
		sum += disp2p(c[stk[i]],c[stk[(i+1)%(top+1)]]);
          printf("%.0lf\n",sum);
	}
return 0;
}


 

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