跳到正文

题解

题解:AT_yuha_c88_g 志なかばで死んだ勇者の名は…

TimADE约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年4月19日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 6 分钟

版本

  1. v1当前

翻译

其实这道题就是:给定一个简单多边形和一组二维平面上的点,要求判断哪些点位于多边形内部(不包括边界),并按坐标顺序输出这些点的名称。

算法:射线法

  1. 基本原理:从待测点向右发射一条水平射线,统计与多边形边的交点数量

    • 奇数:点在内部

    • 偶数:点在外部

  2. 特殊情况处理:

    • 点在边上:直接判定为不在内部

    • 射线与顶点相交:特殊计数规则避免重复计算

工具

  1. 判断点与边的相对位置
C++
double x(const P& a, const P& b) {
    return imag(conj(a) * b);
}
  1. 利用 CCW 判断三点走向
C++
int w(P a, P b, P c) {
    b -= a; c -= a;
    double l = abs(b) * abs(c);
    if(x(b, c) > +E * l) return +1;  // 逆时针
    if(x(b, c) < -E * l) return -1;  // 顺时针
    if(d(b, c) < 0) return +2;       // 共线且在延长线上
    if(norm(b) < norm(c)) return -2;  // 共线且在线段外
    return 0;                         // 共线且在线段上
}

关键代码

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define F(i, n) for(int i = 0; i < (n); i++)
#define A(v) (v).begin(), (v).end()
#define fi F
#define se S
const int I = 1 << 29;
const double E = 1e-9;
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){ F(i, v.size()) { if(v[i] == I) o << "I"; else o << v[i]; if(i != v.size() - 1) o << ' '; } return o; }
const int M = I;
#define C(P, i) (P[(i) % P.size()])
#define N(P, i) (P[((i) + 1) % P.size()])
typedef complex<double> P;
typedef vector<P> G;
struct L : public vector<P> {
    L() {;}
    L(P a, P b) { push_back(a); push_back(b); }
};
struct C {
    P p;
    double r;
    C() {;}
    C(P p, double r) : p(p), r(r) {;}
};
namespace std{
    bool operator < (const P& a, const P& b){
        return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();
    }
}
inline double x(const P& a, const P& b){
    return imag(conj(a) * b);
}
inline double d(const P& a, const P& b){
    return real(conj(a) * b);
}
int w(P a, P b, P c){
    b -= a; c -= a;
    double l = abs(b) * abs(c);
    if(x(b, c) > +E * l) return +1;
    if(x(b, c) < -E * l) return -1;
    if(d(b, c) < 0) return +2;
    if(norm(b) < norm(c)) return -2;
    return 0;
}
enum { O, N, I };
int c(const G& g, const P& p) {
    bool f = false;
    for (int i = 0; i < (int)g.size(); ++i) {
        P a = C(g,i) - p, b = N(g,i) - p;
        if (imag(a) > imag(b)) swap(a, b);
        if (imag(a) <= 0 && 0 < imag(b))
            if (x(a, b) < 0) f = !f;
        if (x(a, b) == 0 && d(a, b) <= 0) return N;
    }
    return f ? I : O;
}
int main() {
    std::ios_base::sync_with_stdio(0);
    int n, m;
    cin >> n;
    vector<P> v;
    string s;
    map<string, P> k;
    map<P, string> r;
    double x, y;
    for(int i = 0; i < n; i++) {
        cin >> s >> x >> y;
        P p = P(x, y);
        v.push_back(p);
        r[p] = s;
        k[s] = p;
    }
    cin >> m;
    G g, a;
    for(int i = 0; i < m; i++) {
        cin >> s;
        g.push_back(k[s]);
    }
    for(int i = 0; i < n; i++) {
        if(c(g, v[i]) == I) {
            a.push_back(v[i]);
        }
    }
    sort(a.begin(), a.end());
    for(int i = 0; i < a.size(); i++) {
        cout << r[a[i]] << endl;
    }
}
原文