POJ 1696 卷包裹算法

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
const int maxn = 100 + 10;
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;
typedef vector<Point> Polygon;
Vector operator +(Vector A, Vector B)//
{
	return Vector(A.x + B.x, A.y + B.y);
}
Vector operator -(Point A, Point B)//
{
	return Vector(A.x - B.x , A.y - B.y);
}
Vector operator *(Vector A, double p)//
{
	return Vector(A.x * p, A.y * p);
}
Vector operator /(Vector A, double p)//
{
	return Vector(A.x / p, A.y / p);
}
bool operator <(const Point &a, const Point &b)//
{
	return a.y < b.y || (a.y == b.y && a.x < b.x);
}
const double eps = 1e-10;
int dcmp(double x)//
{
	if (fabs(x) < eps) return 0;
	else return x < 0 ? -1 : 1;
}
bool operator ==(const Point &a, const Point &b)//
{
	return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
double Dot(Vector A, Vector B)//
{
	return A.x * B.x + A.y * B.y;
}
double Length(Vector A)//
{
	return sqrt(Dot(A, A));
}
double Cross(Vector A, Vector B)//
{
	return A.x * B.y - A.y * B.x;
}
Point read_point()
{
	double X, Y;
	scanf("%lf%lf", &X, &Y);
	return Point(X, Y);
}
int T, t, n;
Point P[maxn];
void extend_ConvexHull(Point *p, int n)
{
	map<Point, int>mp;
	for (int i = 0; i < n; i++)
		mp[p[i]] = i;
	bool vis[maxn];
	memset(vis, 0, sizeof(vis));
	sort(p, p + n);
	vector<int>ans;
	ans.push_back(mp[p[0]]); vis[0] = 1;
	int next = 0, cur = 0;
	for (int m = 1; m < n; m++)
	{
		for (int i = 0, first = 1; i < n; i++)
		{
			if (vis[i]) continue;
			if (first) next = i, first = 0;
			if (dcmp(Cross(p[next] - p[cur], p[i] - p[cur]) < 0) || (dcmp(Cross(p[next] - p[cur], p[i] - p[cur]) == 0) && dcmp(Length(p[i] - p[cur]) - Length(p[next] - p[cur])) < 0))
				next = i;
		}
		vis[next] = 1; cur = next;
		ans.push_back(mp[p[next]]);
	}
	printf("%d ", n);
	for (int i = 0; i < n; i++)
		printf("%d%c", ans[i] + 1, (i == n - 1 ? '\n' : ' '));
}
int main(int argc, char const *argv[])
{
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d", &n);
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &t);
			P[i] = read_point();
		}
		extend_ConvexHull(P, n);
	}
	return 0;
}


要求的是原来的标号,这个相当恶心,搞了一个map来存的。

很显然,这不是要求凸包,但只要把卷包裹算法修改一下就可以了。

卷包裹算法:http://www.cnblogs.com/Booble/archive/2011/02/28/1967179.html

凸包的五大算法:http://blog.csdn.net/bone_ace/article/details/46239187

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值