HDU - 6590 判断两凸包是否相交

6590

好像数据有点水, 我没判点是否在凸包内。。

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 100 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const double PI = acos(-1);

template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int dcmp(double x) {
    if(fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}

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

double dist(const Point& a, const Point& b) {
    return sqrt((a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y));
}

typedef Point Vector;

Point operator + (Vector A, Vector B) {return Point(A.x + B.x, A.y + B.y);}
Point operator - (Vector A, Vector B) {return Point(A.x - B.x, A.y - B.y);}
Point operator * (Vector A, double p) {return Point(A.x * p, A.y * p);}
Point operator / (Vector A, double p) {return Point(A.x / p, A.y / p);}
bool operator < (const Vector &A, const Vector &B) {return A.x < B.x || (A.x == B.x && A.y < B.y);}
bool operator == (const Vector &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 Angle(Vector A, Vector B) {return acos(Dot(A, B)/Length(A)/Length(B));}
double Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;}
double Area2(Point A, Point B, Point C) {return Cross(B-A, C-A);}

int ConvexHull(vector<Point>& p, vector<Point>& ch) {
    int n = p.size(), m = 0;
    sort(p.begin(), p.end());
    for(int i = 0; i < n; i++) {
        while(m > 1 && dcmp(Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])) <= 0) ch.pop_back(), m--;
        ch.push_back(p[i]); m++;
    }
    int k = m;
    for(int i = n - 2; i >= 0; i--) {
        while(m > k && dcmp(Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])) <= 0) ch.pop_back(), m--;
        ch.push_back(p[i]); m++;
    }

    return m;
}

int n;
vector<Point> p[2], ch[2];

bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2) {
    double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);
    double c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
    return dcmp(c1) * dcmp(c2) <= 0 && dcmp(c3) * dcmp(c4) <= 0
            && max(b1.x, b2.x) >= min(a1.x, a2.x) && max(a1.x, a2.x) >= min(b1.x, b2.x)
            && max(b1.y, b2.y) >= min(a1.y, a2.y) && max(a1.y, a2.y) >= min(b1.y, b2.y);
}

int main() {
    int T; scanf("%d", &T);
    while(T--) {
        p[0].clear();
        p[1].clear();
        ch[0].clear();
        ch[1].clear();
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) {
            int x1, x2, y;
            scanf("%d%d%d", &x1, &x2, &y);
            if(y > 0) {
                p[0].push_back(Point(x1, x2));
            }
            else {
                p[1].push_back(Point(x1, x2));
            }
        }

        ConvexHull(p[0], ch[0]);
        ConvexHull(p[1], ch[1]);
        bool judge = true;

        for(int i = 0; i < SZ(ch[0]); i++) {
            int j = (i + 1) % SZ(ch[0]);
            for(int u = 0; u < SZ(ch[1]); u++) {
                int v = (u + 1) % SZ(ch[1]);
                if(SegmentProperIntersection(ch[0][i], ch[0][j], ch[1][u], ch[1][v])) {
                    judge = false;
                    i = SZ(ch[0]);
                    break;
                }
            }
        }
        puts(judge ? "Successful!" : "Infinite loop!");
    }
    return 0;
}

/*
*/

 

posted @ 2019-07-23 13:27  NotNight  阅读(638)  评论(0编辑  收藏  举报