poj 1113 Wall

大致题意:给定多边形城堡的n个顶点,绕城堡外面建一个围墙,围住所有点,并且墙与所有点的距离至少为L,求这个墙最小的长度。

解题思路:者很明显是一个凸包问题,围墙的长度=整个凸包的长度+pi*2*L


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;

struct Point
{
	int x;
	int y;
};

int N, d; // 顶点数
Point shape[1000]; // 记录多边形各个顶点
Point bulgeShape[1001]; // (栈)记录凸包中各顶点

int top; // 栈顶

// 返回两点间距离
double Dis( const Point & p1, const Point & p2 )
{
	return sqrt( (double)((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)) );
}

// p2为交点, 返回p1在p2的什么方向
int Mul( const Point & p1, const Point & p2, const Point & p3 )
{
	return ( p1.x - p2.x ) * ( p3.y - p2.y ) - ( p3.x - p2.x ) * ( p1.y - p2.y );
}

/*
*  返回真表示p1与shape[0]的极角比p2与shape[0]的极角大
*/
bool cmp ( const Point  p1, const Point  p2 )
{
   return Mul( p1, shape[0], p2 ) > 0;
}

//寻找多边形shape的凸包,将凸包记录在bulgeShape中
void Graham_Scan()
{
	/* 寻找多边形的最低点,若有多个,则找最左边的点 */

	int i;
	int pos = 0;

	for( i = 1; i < N; ++ i )
	{
		if( shape[i].y < shape[pos].y || 
			shape[i].y == shape[pos].y && shape[i].x < shape[pos].x )
		pos = i;
	}

	Point temp = shape[0];
	shape[0] = shape[pos];
	shape[pos] = temp;

	/* 对多边形中所有顶点按与shape[0]的极角大小从小到大排序(不考虑极角相等的情况) */

	sort( shape + 1, shape + N, cmp );

	/* 找凸包 */

	top = 0; // 置栈顶为空

	bulgeShape[top++] = shape[0];
	bulgeShape[top++] = shape[1];

	for( i = 2; i < N; ++ i )
	{
		/* 下面这句判断3点共线 */
		if(  Mul( shape[i], bulgeShape[top-1], bulgeShape[top-2] ) == 0 &&                    // 三点共线
			( (shape[i].x - bulgeShape[top-2].x) * (shape[i].x - bulgeShape[top-1].x) < 0 || // 且新点在另两点之间
			  (shape[i].y - bulgeShape[top-2].y) * (shape[i].y - bulgeShape[top-1].y) < 0 ) )
		{
			continue;
		}
		while( top - 2 >= 0 && Mul( shape[i], bulgeShape[top-1], bulgeShape[top-2] ) <= 0 )
		{
			-- top;
		}
		bulgeShape[top++] = shape[i];
	}

}

int main()
{
	freopen("test.txt","r",stdin);
	int i;
	cin >> N >> d;
	for( i = 0; i < N; ++ i )
		cin >> shape[i].x >> shape[i].y;

	Graham_Scan();
	bulgeShape[top] = bulgeShape[0];
	double tempSum = 0;
	for( i = 0; i < top; ++ i )
	{
		tempSum += Dis( bulgeShape[i], bulgeShape[i+1] );
	}
	tempSum += 2 * 3.141592654 * d;
	int sum = (int)( tempSum + 0.5 );
	cout << sum << endl;
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值