POJ 3335 Rotating Scoreboard (半平面交)

题目:传送门

题意:给你一个 n 个节点的多边形,问你是否至少存在一个点,使得在这个点可以看见多边形的边界上的所有点。

 

思路:

这题本质上就是求半平面交。

关于半平面交的讲解可以看这个博客:

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>

#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
#define fir first
#define sec second
using namespace std;

const int N = 1e2 + 5;
const double eps = 1e-8;

struct Point {
    double x, y;
    Point(double x = 0, double y = 0) : x(x), y(y) { }
};

Point operator + (Point A, Point B) { return Point(A.x + B.x, A.y + B.y); }
Point operator - (Point A, Point B) { return Point(A.x - B.x, A.y - B.y); }
Point operator * (Point A, double p) { return Point(A.x * p, A.y * p); }
Point operator / (Point A, double p) { return Point(A.x / p, A.y / p); }

double Cross(Point A, Point B) { return A.x * B.y - A.y * B.x; }
double Dot(Point A, Point B) { return A.x * B.x + A.y * B.y; }

/* 有向直线,它的左边就是对应的半平面 */
struct Line {
    Point p; /// 直线任意一点
    Point v; /// 方向向量
    double ang; /// 极角,即从x正半轴旋转到向量v所需要的角(弧度)
    Line() { }
    Line(Point p, Point v) : p(p), v(v) { ang = atan2(v.y, v.x); }
    bool operator < (const Line& L) const {
        return ang < L.ang;
    }
};

/* 点p在有向直线L的左边 */
bool OnLeft(Line L, Point p) {
    return Cross(L.v, p - L.p) >= 0;
}

/* 二直线交点,假设交点唯一存在。*/
Point GLI(Line a, Line b) {
    Point u = a.p - b.p;
    double t = Cross(b.v, u) / Cross(a.v, b.v);
    return a.p + a.v * t;
}

int HPI(Line* L, int n, Point* Q) {
    sort(L, L + n); /// 极角排序

    int st, ed; /// 双端队列的第一个元素和最后一个元素的下标

    Point *p = new Point[n]; /// p[i]为q[i]和q[i+1]的交点
    Line *q = new Line[n]; /// 双端队列
    q[st = ed = 0] = L[0];

    rep(i, 1, n - 1) {
        while(st < ed && !OnLeft(L[i], p[ed - 1])) ed--;
        while(st < ed && !OnLeft(L[i], p[st])) st++;

        q[++ed] = L[i];
        
        /// 平行取内测那条
        if(fabs(Cross(q[ed].v, q[ed - 1].v)) < eps) {
            ed--;
            if(OnLeft(q[ed], L[i].p)) q[ed] = L[i];
        }

        if(st < ed) p[ed - 1] = GLI(q[ed - 1], q[ed]);

    }

    while(st < ed && !OnLeft(q[st], p[ed - 1])) ed--;

    if(ed - st <= 1) return 0;

    p[ed] = GLI(q[ed], q[st]);

    int m = 0;
    rep(i, st, ed) Q[m++] = p[i];
    return m;
}

Point P[N], Q[N];

Line L[N];

void solve() {
    int n;
    scanf("%d", &n);
    rep(i, 0, n - 1) scanf("%lf %lf", &P[i].x, &P[i].y);
    reverse(P, P + n);
    rep(i, 0, n - 1) L[i] = Line(P[i], P[(i + 1) % n] - P[i]);

    int ans = HPI(L, n, Q);

    if(ans == 0) puts("NO");
    else puts("YES");
}

int main() {

    int _; scanf("%d", &_);
    while(_--) solve();

    return 0;
}

 

posted on 2020-03-10 16:31  Willems  阅读(91)  评论(0编辑  收藏  举报

导航