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;
}
时间: 2024-12-31 04:05:01

POJ 1127 Jack Straws (线段相交)的相关文章

poj 1127 Jack Straws 线段相交+并查集

题意: 有n个木棍,给出木棍的两个端点的x,y坐标,判断其中某两个线段是否连通(可通过其他线段连通) #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <map> #include <

poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)

Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table and players try to remove them one-by-one without disturbing the other straws. Here, we are only concerned with if various pairs of straws are

poj1127 Jack Straws(线段相交+并查集)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3512   Accepted: 1601 Description In the game of Jack Straws, a number of plastic or wooden "straws" are dumped o

POJ 1127 Jack Straws (计算几何)

Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3945   Accepted: 1787 Description In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table and players try to remove them one-by-one witho

POJ 1127 Jack Straws 几何基础

Description In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table and players try to remove them one-by-one without disturbing the other straws. Here, we are only concerned with if various pairs of straws are

POJ 1127 Jack Straws ( 求直线交点, 判断线段是否相交(含端点) )

题目:传送门 题意: 给你 n 条线段的两个端点, 然后有多次询问, 每次询问, 问你线段 x 和 线段 y 是否相交. 若线段 A 和线段 B 相交且线段 A 和线段 C 相交,那么线段 B 和线段 C 相交.     1 < n < 13 题解: 暴力求线段是否相交, 然后再跑个 Floyd 或者并查集都可以的. #include <iostream> #include <stdio.h> #include <string.h> #include <

POJ - 1127 Jack Straws(几何)

题意:桌子上放着n根木棍,已知木棍两端的坐标.给定几对木棍,判断每对木棍是否相连.当两根木棍之间有公共点或可以通过相连的木棍间接的连在一起,则认为是相连的. 分析: 1.若线段i与j平行,且有部分重合,则相连.否则,求直线i与直线j交点,再判断该交点是否在两线段上,以确定是否相连. 2.flod整理一下所有的关系. 3.判断点是否在线段上:(线段i,点t) (1)外积(l[i] - t) × (r[i] - t) = 0, 可判断点x是否在直线i上(两向量叉乘为0,两向量平行) (2)内积(l[

poj 3304 直线与线段相交

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12161   Accepted: 3847 Description Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments

POJ 1410 Intersection(线段相交&amp;&amp;判断点在矩形内&amp;&amp;坑爹)

Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段完全在矩形内部算相交:线段与矩形任意一条边不规范相交算相交. 思路:知道具体的相交规则之后题其实是不难的,但是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,需要重新判断一下...真坑. 1 struct Point 2 { 3 double x, y; 4 } A, B, C, D; 5 struct Line 6 { 7 Point a, b; 8 } L; 9 10 int n; 11