首页 > 代码库 > POJ 1127 Jack Straws (线段相交)
POJ 1127 Jack Straws (线段相交)
题意:给定一堆线段,然后有询问,问这两个线段是不是相交,并且如果间接相交也可以。
析:可以用并查集和线段相交来做,也可以用Floyd来做,相交就是一个模板题。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 100 + 10; const int mod = 1e9 + 7; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } struct Point{ double x, y; Point(double x = 0, double y = 0) : x(x), y(y) { } }; typedef Point Vector; int dcmp(double x){ if(fabs(x) < eps) return 0; return x < 0 ? -1 : 1; } Vector operator + (Vector A, Vector B){ return Vector(A.x+B.x, A.y+B.y); } Vector operator - (Vector A, Vector 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); } 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 Cross(Vector A, Vector B){ return A.x*B.y - A.y*B.x; } struct Node{ Point p, q; }; Node a[maxn]; int p[maxn]; int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); } bool onSegment(Point p, Point a1, Point a2){ return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0; } bool judge(int i, int j){ if(onSegment(a[i].p, a[j].p, a[j].q)) return true; if(onSegment(a[i].q, a[j].p, a[j].q)) return true; if(onSegment(a[j].p, a[i].p, a[i].q)) return true; if(onSegment(a[j].q, a[i].p, a[i].q)) return true; if(a[i].p == a[j].q || a[i].p == a[j].p || a[i].q == a[j].p || a[i].q == a[j].q) return true; double c1 = Cross(a[i].q-a[i].p, a[j].p-a[i].p); double c2 = Cross(a[i].q-a[i].p, a[j].q-a[i].p); double c3 = Cross(a[j].q-a[j].p, a[i].p-a[j].p); double c4 = Cross(a[j].q-a[j].p, a[i].q-a[j].p); return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0; } int main(){ while(scanf("%d", &n) == 1 && n){ for(int i = 1; i <= n; ++i){ p[i] = i; scanf("%lf %lf %lf %lf", &a[i].p.x, &a[i].p.y, &a[i].q.x, &a[i].q.y); } for(int i = 1; i <= n; ++i){ int x = Find(i); for(int j = 1 + i; j <= n; ++j){ int y = Find(j); if(x == y) continue; if(judge(i, j)) p[y] = x; } } int x, y; while(scanf("%d %d", &x, &y) == 2 && x+y){ x = Find(x); y = Find(y); if(x == y) puts("CONNECTED"); else puts("NOT CONNECTED"); } } return 0; }
POJ 1127 Jack Straws (线段相交)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。