您的位置:首页 > 其它

POJ 1696 卷包裹算法

2016-03-16 16:51 323 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: