POJ 1113 解题报告

为了以后搜索方便,注:这道题是凸包问题。用的是convex hull的扫描算法。

这道题实际上是个convex hull的问题。因为无论有些点凹进去多少,实际上是由那些convex hull上的凸点决定了最短的边界的形状。所以最终的围栏周长是convex hull的周长加上一个完整的园的周长(半径就是L)。

代码用的是我之前的代码:http://blog.csdn.net/thestoryofsnow/article/details/38516855

1113Accepted180K16MSC++2488B

/* 
ID: thestor1 
LANG: C++ 
TASK: poj1113 
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cassert>

using namespace std;

class Point{
public:
	int x, y;
	
	Point()
	{
		Point(0, 0);
	}
	
	Point (int x, int y)
	{
		this->x = x;
		this->y = y;
	}

	bool operator <(const Point &p) const {
		return x < p.x || (x == p.x && y < p.y);
	}

	friend ostream& operator<<(ostream& os, const Point& p);
};

ostream& operator<<(ostream& os, const Point& p)
{
	os << p.x << "\t" << p.y;
	return os;
}

int cross(const Point &left, Point &middle, Point &right)
{
	return (left.x - middle.x) * (right.y - middle.y) - (right.x - middle.x) * (left.y - middle.y);
}

int crossProduct(int x1, int y1, int x2, int y2)
{
	return x1 * y2 - x2 * y1;
}

double dis(Point p1, Point p2)
{
	return sqrt((double)((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)));
}

double convexhull(std::vector<Point> &points)
{
	sort(points.begin(), points.end());

	// for (int i = 0; i < points.size(); ++i)
	// {
	// 	cout<<points[i]<<endl;
	// }

	std::vector<int> convexhull(2 * points.size());
	int k = 0;
	for (int i = 0; i < points.size(); ++i)
	{
		while (k >= 2 && cross(points[convexhull[k - 2]], points[convexhull[k - 1]], points[i]) <= 0)
		{
			k--;
		}
		convexhull[k] = i;
		k++;
	}

	for (int i = points.size() - 2, t = k + 1; i >= 0; --i)
	{
		while (k >= t && cross(points[convexhull[k - 2]], points[convexhull[k - 1]], points[i]) <= 0)
		{
			k--;
		}
		convexhull[k] = i;
		k++;	
	}
	convexhull.resize(k);

	// cout<<"convexhull:"<<endl;
	// for (int i = 0; i < convexhull.size(); ++i)
	// {
	// 	cout<<points[convexhull[i]]<<endl;
	// }

	double perimeter = 0.00;
	for (int i = 0; i < convexhull.size() - 1; ++i)
	{
		perimeter += dis(points[convexhull[i]], points[convexhull[i + 1]]);
	}

	// cout << "perimeter:" << perimeter << endl;
	return perimeter;
}

int main()
{
	int N, L;	
	scanf("%d%d", &N, &L);
	std::vector<Point> points(N);
	for (int i = 0; i < N; ++i)
	{
		scanf("%d%d", &points[i].x, &points[i].y);
	}
	double perimeter = convexhull(points);
	double circle = 2 * 3.141592653 * L;
	printf("%.0f\n", perimeter + circle);
	return 0;  
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值