POJ 1696

题目思路还是很巧妙的…题解可以看这个:https://blog.csdn.net/u013480600/article/details/40125055
总体而言就是一个类凸包的思路,但是不同点在于凸包只按初始的横坐标最小的点排一次极角序,然后对于后面每个点判断这个点是否在凸包上。而这里每次都把当前极角序最小的点加入答案,然后将后面的点按照这个点为初始点再排一次序。另外,此题找最初始点的时候根据题目描述,应该是y坐标小的优先,y相同时x小的优先,跟平时的凸包模板有差异。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
const int MAXN = 100000;
const double EPS = 1e-8;

// 带误差比较
inline bool dcmp(double x, double y = 0)
{
    return fabs(x - y) <= EPS;
}

/*
 * 向量(Vector)或点
 *
 * 使用原点到一个点 (x, y) 的有向线段表示向量
 * 从点 A 到点 B 的向量表示为 A - B
 */
typedef struct Vec
{
    double x, y;
    int id;
    Vec(double x = 0, double y = 0) : x(x), y(y) {}

    // 相加
    Vec operator+(const Vec &v) const
    {
        return Vec(x + v.x, y + v.y);
    }

    // 相减
    Vec operator-(const Vec &v) const
    {
        return Vec(x - v.x, y - v.y);
    }

    // 数乘(伸长、缩短)
    Vec operator*(double d) const
    {
        return Vec(x * d, y * d);
    }

    Vec operator/(const double d) const
    {
        return Vec(x / d, y / d);
    }

    // 范数,用来比较长度,等于长度的平方
    double norm() const
    {
        return x * x + y * y;
    }
} Pt;

// 点乘
double dot(const Vec &a, const Vec &b)
{
    return a.x * b.x + a.y * b.y;
}

// 叉乘
double cross(const Vec &a, const Vec &b)
{
    return a.x * b.y - a.y * b.x;
}

// 线段(Segment),用两个点表示
struct Seg
{
    Pt a, b;

    Seg(const Pt &a, const Pt &b) : a(a), b(b) {}

    // 线段包含点(点在线段上)
    bool include(const Pt &p)
    {
        // PA × PB = 0:PA 与 PB 共线,即点在线段所在的直线上
        // PA · PB = 0:PA 与 PB 方向不同(A 和 B 分别在 P 的两边),如果 PA · PB = 0 则 P = A 或 P = B
        return dcmp(cross(a - p, b - p)) && dot(a - p, b - p) <= 0;
    }
};

// 直线,用两个点表示
struct Line
{
    Pt a, b;

    Line() {} // 提供一个不需要参数的构造函数
    Line(const Pt &a, const Pt &b) : a(a), b(b) {}

    bool include(const Pt &p) const
    {
        return dcmp(cross(a - p, b - p));
    }

    // 两直线关系(交点个数)
    // 0 表示平行(无交点)
    // 1 表示相交(一个交点)
    // -1 表示重合(无数个交点)
    static int relation(const Line &a, const Line &b)
    {
        if (a.include(b.a) && a.include(b.b)) return -1;
        else if (dcmp(cross(a.b - a.a, b.b - b.a))) return 0;
        else return 1;
    }

    // 求两直线交点(需要保证两直线有交点)
    static Pt intersect(const Line &a, const Line &b)
    {
        double s1 = cross(b.a - a.a, b.b - a.a), s2 = cross(b.b - a.b, b.a - a.b);
        return a.a + (a.b - a.a) * s1 / (s1 + s2);
    }
};

// 求凸包用的点
int n;
Pt a[MAXN + 1],cur;

// 凸包极角排序的比较函数
inline bool compare(const Pt &a, const Pt &b)
{
    // 两个向量
    Vec va = a - cur, vb = b - cur;
    double t = cross(va, vb);
    if (!dcmp(t)) return t > 0; // OA -> OB 是逆时针,则 A 极角序在先
    else return va.norm() < vb.norm(); // norm 较小的长度较小
}

struct Poly
{
    std::vector<Pt> pts;

    // 求凸包(Convex),结果储存在自身 pts 中
    void convex()
    {
        // 找出最左下角的点
        int id = 1;
        for (int i = 1; i <= n; i++)
        {
            if (a[i].y < a[id].y || (a[i].y == a[id].y && a[i].x < a[id].x)) id = i;//最初始点与平时有不同
        }
        if (id != 1) swap(a[1], a[id]);

        // 排序
        //std::sort(a + 2, a + n + 1, &compare);
        pts.push_back(a[1]);cur=a[1];
        for (int i = 2; i <= n; i++)
        {
            sort(a+i,a+1+n,&compare);cur=a[i];//每次都要排序
            pts.push_back(a[i]);
        }
    }
};
int main()
{
    int m,i,j;
    cin>>m;
    while(m--){
        cin>>n;
        for(i=1;i<=n;i++){
            scanf("%d%lf%lf",&a[i].id,&a[i].x,&a[i].y);
        }
        Poly p;p.convex();cout<<p.pts.size();
        for(i=0;i<p.pts.size();i++)printf(" %d",p.pts[i].id);
        cout<<endl;
    }

    return  0;
}

猜你喜欢

转载自blog.csdn.net/humveea6/article/details/80304155
POJ