Poj 1113 Wall(凸包)

博客介绍了POJ 1113题目的解题思路,重点讨论了如何处理城堡各点构成的凸包问题。内容涵盖了凸包周长的计算,以及在编程实现中需要注意的细节,如四舍五入处理、浮点型参数的使用和精度问题。此外,还提供了一组通过测试的数据和AC代码。
摘要由CSDN通过智能技术生成

题目链接

解析:凸包问题,城堡各点构成的一个凸包,结果就是凸包周长加上一个以要求的距离为半径的圆周(任意凸多边形的外角和都是360^{\circ}

总结:1. 成员函数的构造。 2. 四舍五入:(int)(ans + 0.5)。  3. sqrt的参数必须是浮点型。 4. 即使输入的是整数,在计算边长还是会变成小数,用int会影响精度问题。 5. 一组测试数据 

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 1005;
int n, l;
struct Point
{
	int x, y;
	Point(int x=0, int y=0): x(x), y(y) {};   //方便后面(,)形式的书写 
	bool operator < (const Point& a) const 
	{
		return (x < a.x) || (x == a.x && y < a.y);
	}
}ch[maxn], p[maxn];
typedef Point Vector;
Vector operator - (Point a, Point b){
	return Vector(a.x-b.x, a.y-b.y);
}
double Dot(Vector a, Vector b){
	return a.x*b.x + a.y*b.y;
} 
double Length(Vector a){
	return sqrt(Dot(a, a));
} 
int Cross(Vector a, Vector b){
	return a.x*b.y - a.y*b.x;
}
int ConvexHull(Point* p, Point* ch)
{
	sort(p, p+n); 
	int top = 0;
	for(int i = 0; i < n; ++ i)
	{
		while(top > 1 && Cross(ch[top-1]-ch[top-2], p[i]-ch[top-2]) <= 0) top --;
		ch[top ++] = p[i];
	}
	int k = top;
	for(int i = n-2; i >= 0; -- i)
	{
		while(top > k && Cross(ch[top-1]-ch[top-2], p[i]-ch[top-2]) <= 0) top --;
		ch[top ++] = p[i]; 
	}
	if(n > 1) top --;
	return top;
}
void solve(int top)
{
	double ans = 0;
	for(int i = 0; i < top; ++ i){
		ans += Length(ch[(i+1)%top]-ch[i]);
	}
	ans += 3.1415926*2*l;
	printf("%d\n",(int)(ans + 0.5));
}
int main()
{
	scanf("%d%d", &n, &l);
	for(int i = 0; i < n; ++ i)
	{
		scanf("%d%d", &p[i].x, &p[i].y);
	}
	int cnt = ConvexHull(p, ch);
	solve(cnt);
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值