Jack Straws
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5428   Accepted: 2461

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 connected by a path of touching straws. You will be given a list of the endpoints for some straws (as if they were dumped on a large piece of graph paper) and then will be asked if various pairs of straws are connected. Note that touching is connecting, but also two straws can be connected indirectly via other connected straws. 

Input

Input consist multiple case,each case consists of multiple lines. The first line will be an integer n (1 < n < 13) giving the number of straws on the table. Each of the next n lines contain 4 positive integers,x1,y1,x2 and y2, giving the coordinates, (x1,y1),(x2,y2) of the endpoints of a single straw. All coordinates will be less than 100. (Note that the straws will be of varying lengths.) The first straw entered will be known as straw #1, the second as straw #2, and so on. The remaining lines of the current case(except for the final line) will each contain two positive integers, a and b, both between 1 and n, inclusive. You are to determine if straw a can be connected to straw b. When a = 0 = b, the current case is terminated.

When n=0,the input is terminated.

There will be no illegal input and there are no zero-length straws.

Output

You should generate a line of output for each line containing a pair a and b, except the final line where a = 0 = b. The line should say simply "CONNECTED", if straw a is connected to straw b, or "NOT CONNECTED", if straw a is not connected to straw b. For our purposes, a straw is considered connected to itself. 

Sample Input

7
1 6 3 3
4 6 4 9
4 5 6 7
1 4 3 5
3 5 5 5
5 2 6 3
5 4 7 2
1 4
1 6
3 3
6 7
2 3
1 3
0 0 2
0 2 0 0
0 0 0 1
1 1
2 2
1 2
0 0 0

Sample Output

CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
CONNECTED
CONNECTED 题意:问两条线段是否连通,通过第三条线段连通也算连通
题解:几何计算的模版加并查集,用floyd算法应该也可以吧
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=;
const double eps=1e-; //考虑误差的加法运算
double add(double x,double y)
{
if(abs(x+y)<eps*(abs(x)+abs(y)))
return ;
return x+y;
} //二维向量结构体
struct P
{
double x,y;
P() {}
P(double x,double y):x(x),y(y){}
P operator+(P p)
{
return P(add(x,p.x),add(y,p.y));
}
P operator-(P p)
{
return P(add(x,-p.x),add(y,-p.y));
}
P operator*(double d)
{
return P(x*d,y*d);
}
double dot(P p) //内积
{
return add(x*p.x,y*p.y);
}
double det (P p) //外积
{
return add(x*p.y,-y*p.x);
}
}; //判断点是否在直线上
bool on_seg(P p1,P p2,P q)
{
return (p1-q).det(p2-q)== && (p1-q).dot(p2-q)<=;
} //计算直线p1-p2与直线q1-q2的交点
P inter(P p1,P p2,P q1,P q2)
{
return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
} int n;
P p[maxn],q[maxn]; //保存一条线段的两个端点
bool G[maxn][maxn]; //线段之间是否联通的图 int main()
{
while(cin>>n && n)
{
memset(G,false,sizeof(G));
for(int i=;i<n;i++)
cin>>p[i].x>>p[i].y>>q[i].x>>q[i].y; for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
if((p[i]-q[i]).det(p[j]-q[j])==)
{
G[i][j]=G[j][i]=on_seg(p[i], q[i], p[j])
|| on_seg(p[i], q[i], q[j])
|| on_seg(p[j], q[j], p[i])
|| on_seg(p[j], q[j], q[i]);
}
else
{
P r=inter(p[i], q[i], p[j], q[j]);
G[i][j]=G[j][i]=on_seg(p[i], q[i], r) && on_seg(p[j], q[j], r);
}
} for(int k=;k<n;k++)
for(int i=;i<n;i++)
for(int j=;j<n;j++)
G[i][j] |=G[i][k] && G[k][j];
int x,y;
while(cin>>x>>y && (x||y))
{
x--;
y--;
if(G[x][y])
cout<<"CONNECTED"<<endl;
else
cout<<"NOT CONNECTED"<<endl;
}
}
return ;
}

Jack Straws POJ - 1127 (几何计算)的更多相关文章

  1. Jack Straws POJ - 1127 (简单几何计算 + 并查集)

    In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table ...

  2. Jack Straws(POJ 1127)

    原题如下: Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5555   Accepted: 2536 ...

  3. Jack Straws(poj 1127) 两直线是否相交模板

    http://poj.org/problem?id=1127   Description In the game of Jack Straws, a number of plastic or wood ...

  4. poj 1127:Jack Straws(判断两线段相交 + 并查集)

    Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2911   Accepted: 1322 Descr ...

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

    Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...

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

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Jack Straws Time Limit: 1000MS   Memory L ...

  7. 1840: Jack Straws

    1840: Jack Straws 时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte 总提交: 168            测试通过:129 描述 I ...

  8. TZOJ 1840 Jack Straws(线段相交+并查集)

    描述 In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the ta ...

  9. 1549: Navigition Problem (几何计算+模拟 细节较多)

    1549: Navigition Problem Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 256 Mb     Su ...

随机推荐

  1. Linux学习笔记——如何使用echo指令向文件写入内容

    0.前言     本文总结如何使用echo命令向文件中写入内容,例如使用echo指令覆盖文件内容,使用echo指令向文件追加内容,使用echo指令往文件中追加制表符.     echo向文件中输出内容 ...

  2. 利用Vagrant and VirtualBox搭建core os环境

    利用Vagrant and VirtualBox搭建core os环境 系统环境 ubuntu 14.04 x64 vagrant 1.7.4 virtualbox 4.3.10 git 1.9.1 ...

  3. 移动端之js控制rem,适配字体

    方法一:设置fontsize 按照iphone 5的适配  1em=10px    适配320 // “()()”表示自执行函数 (function (doc, win) { var docEl = ...

  4. 现阶段github上的emysql编译无法通过的问题

    最近在写db引擎,今天用到了emysql,找到https://github.com/Eonblast/Emysql,拽下来,然后发现竟然编译不通过~~去网上找了下资料,在 http://erlang. ...

  5. ThreadLocal源码解析,内存泄露以及传递性

    我想ThreadLocal这东西,大家或多或少都了解过一点,我在接触ThreadLocal的时候,觉得这东西很神奇,在网上看了很多博客,也看了一些书,总觉得有一个坎跨不过去,所以对ThreadLoca ...

  6. Android Studio maven-metadata.xml 卡着不动原因和解决方法

    头一天好好的,第二天就卡着了. 一直在这个地方不动,如果停止就会报 Error:Could not run build action using Gradle distribution ‘https: ...

  7. Centos离线安装Docker并加入到Swarm管理节点

    以root用户登录 加入Swarm前需要在Swarm上生成Token,所以需要提前将Swarm集群搭建完成后,再运行以下命令将各虚机加入到swarm节点 下载docker离线安装包,并拷贝到/root ...

  8. CentOS 7安装Docker服务详细过程

    ---恢复内容开始--- Docker 简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟 ...

  9. SharePoint 2013 安装配置(1)

    在这篇文章中,我将逐步介绍在Windows Server 2012 R2上安装SharePoint 2013. 在进一步详细介绍之前,让我们先了解SharePoint 2013安装的硬件和软件要求.您 ...

  10. Spark Job调优(Part 1)

    原文链接:https://wongxingjun.github.io/2016/05/11/Spark-Job%E8%B0%83%E4%BC%98-Part-1/ Spark应用的执行效率是所有程序员 ...