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 (线段相交)的更多相关文章
- [poj 1127]Jack Straws[线段相交][并查集]
题意: 给出一系列线段,判断某两个线段是否连通. 思路: 根据线段相交情况建立并查集, 在同一并查集中则连通. (第一反应是强连通分量...实际上只要判断共存即可, 具体的方向啊是没有关系的..) 并 ...
- TTTTTTTTTTTTTT poj 1127 Jack Straws 线段相交+并查集
题意: 有n个木棍,给出木棍的两个端点的x,y坐标,判断其中某两个线段是否连通(可通过其他线段连通) #include <iostream> #include <cstdio> ...
- poj 1127:Jack Straws(判断两线段相交 + 并查集)
Jack Straws Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2911 Accepted: 1322 Descr ...
- poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)
Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...
- poj1127 Jack Straws(线段相交+并查集)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Jack Straws Time Limit: 1000MS Memory L ...
- TZOJ 1840 Jack Straws(线段相交+并查集)
描述 In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the ta ...
- POJ 1127 Jack Straws(计算几何)
题目链接 抄的模版,居然1Y了.就是简单的线段相交+并查集. #include <iostream> #include <cstring> #include <cstdi ...
- POJ 1127 Jack Straws (计算几何)
[题目链接] http://poj.org/problem?id=1127 [题目大意] 在二维平面中,给出一些木棍的左右端点,当木棍相交或者间接相交时 我们判断其连通,给出一些询问,问某两个木棍是否 ...
- POJ - 1127 Jack Straws(几何)
题意:桌子上放着n根木棍,已知木棍两端的坐标.给定几对木棍,判断每对木棍是否相连.当两根木棍之间有公共点或可以通过相连的木棍间接的连在一起,则认为是相连的. 分析: 1.若线段i与j平行,且有部分重合 ...
随机推荐
- 【BZOJ4653】[Noi2016]区间 双指针法+线段树
[BZOJ4653][Noi2016]区间 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间,使得这 m个区间共同包含 ...
- 交易准实时预警 kafka topic 主题 异常交易主题 低延迟 event topic alert topic 内存 算法测试
https://www.ibm.com/developerworks/cn/opensource/os-cn-kafka/index.html 周 明耀2015 年 6 月 10 日发布 示例:网络游 ...
- centos 7 官网安装 PostgreSQL
https://www.postgresql.org/download/linux/redhat/
- POJ - 1426 Find The Multiple 【DFS】
题目链接 http://poj.org/problem?id=1426 题意 给出一个数 要求找出 只有 0 和 1 组成的 十进制数字 能够整除 n n 不超过 200 十进制数字位数 不超过100 ...
- 让我们再谈谈 iOS 安全
前言 安全方面的话题总是聊不完的.这不,国外一家有名的专门攻击别人的安全公司 Hacking Team 自己被 Hack 了,结果有 400 多 G 的攻击资料泄漏出来,包括一些 0-day 的漏洞. ...
- 使用阿里云maven镜像加速jar包下载
编辑 MAVEN_HOME/conf 文件夹下的 settings.xml,找到 <mirrors> 节点,把下面内容添加在其子节点内: <mirror> <id> ...
- webpack热替换原理
前期准备: const path = require('path'); const HtmlWebpackPlugin= require('html-webpack-plugin'); const C ...
- Android环境下通过C框架层控制WIFI【转】
本文转载自:https://blog.csdn.net/edw200/article/details/52192631 本人是从事Linux嵌入式开发的,安卓wifi控制在安卓JAVA层已经做得非常成 ...
- 无法定位程序输入点glPopAttrib于动态连结库OPENGL.dll上
已经下载glut.lib glut32.lib glut.h glut.dll glut32.dll并放到了相应的文件夹中运行程序时还提示说缺少opengl.dll,我又下载了一个opengl.dll ...
- Linux_学习_01_ 压缩文件夹
二.参考资料 1.Linux下压缩某个文件夹命令