POJ 1113 Wall (凸包)

给一个多边形(可能是凹的),然后给出一个最小周长围住这个多边形,围的墙至少要离这个多边形一定距离。

凸包,最后加上求周长即可。

#include<iostream>
#include<algorithm>
#include<cmath>
#define PI 3.141592653
using namespace std;

typedef struct{
	int x, y;
} POINT;

POINT ch[1000];

double dist(POINT a, POINT b){
	return sqrt(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
}

int crossProduct(POINT o, POINT a, POINT b){
	return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y);
}

bool cmp_position(POINT a, POINT b){
	return a.y < b.y || (a.y == b.y && a.x < b.x);
}

bool cmp_angle(POINT a, POINT b){
	int temp = crossProduct(ch[0], a, b);
	if(temp == 0)
		return (dist(a, ch[0]) < dist(b, ch[0]));
	else
		return temp > 0;
}

void print(POINT a[], int N){
	for(int i = 0; i < N; i++)
		cout << a[i].x << " " << a[i].y << endl;
}

int main(){
	int N, L;
	cin >> N >> L;
	for(int i = 0; i < N; i++)
		cin >> ch[i].x >> ch[i].y;
	swap(ch[0], *min_element(ch, ch + N, cmp_position));
	sort(ch + 1, ch + N, cmp_angle);
	ch[N] = ch[0];
	POINT s[1100];
	int top = 2;
	s[0] = ch[0];
	s[1] = ch[1];
	for(int i = 2; i <= N; i++){
		while(top >= 2 && crossProduct(s[top - 2], s[top - 1], ch[i]) <= 0){
			top--;
		}
		s[top++] = ch[i];
	}
	//print(s, top);
	double result = 0;
	for(int i = 1; i < top; i++)
		result += dist(s[i - 1], s[i]);
	result += PI * L * 2;
	cout << (int)(result + 0.5) << endl;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值