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. [干货分享]AXURE整套高保真UI框架和元件组(白色风格)

      写在前面 强烈建议开始之前阅读以下第一篇高保真UI框架的前面部分,以了解设计思想,这篇文章不再重复介绍: AXURE-整套可复用的高保真元件和框架之暗黑风格 本次共享模板的UI规范 注:由于篇幅问 ...

  2. P3930 SAC E#1 - 一道大水题 Knight

    TLE,额 ,有空再写吧. #include<queue> #include<cstdio> #include<vector> #include<algori ...

  3. java 与 c#的 中 字符串比较“==”与“equals”的差异

    .net中,其字符串特有的驻留机制,保证了在同一进程中,相同字符序列的字符串,只有一个实例,这样能避免相同内容的字符串重复实例化,以减少性能开销. 先来回顾一下c#中的代码: public stati ...

  4. fastjson格式化输出内容

    引入fastjson <!--fastjson--><dependency> <groupId>com.alibaba</groupId> <ar ...

  5. Mysql order by 排序 varchar 类型数据

    Mysql order by 排序 varchar 类型数据 varchar 类型字段排序,  会將数字当成字符串来处理.  排序规则一般是从左到右一位位来比较. +0之后 就转化成INT 类型排序 ...

  6. <Android 应用 之路> 天气预报(一)

    Android天气预报客户端 设计思路 欢迎界面,版本号,应用名 + 数据后台加载(所有城市的信息获取) 数据加载完成后跳转到显示界面,显示所有查询到的城市的天气信息 欢迎界面和天气显示界面分别为单独 ...

  7. 对比java和python对比

    对比java和python 对比java和python 2011年04月18日 1.难易度而言.python远远简单于java. 2.开发速度.Python远优于java 3.运行速度.java远优于 ...

  8. SingletonLoginUser

    package cn.com.jgt.view{ import flash.errors.IllegalOperationError; /** * actionscript类的构造方法不能是priva ...

  9. BestCoder Round #56 1002 Clarke and problem 1003 Clarke and puzzle (dp,二维bit或线段树)

    今天第二次做BC,不习惯hdu的oj,CE过2次... 1002 Clarke and problem 和Codeforces Round #319 (Div. 2) B Modulo Sum思路差不 ...

  10. MAC OSXU盘会挂载目录

    当U盘接到系统后,你可以在Terminal里输入df -lh.这时,硬盘的使用和分区情况会输出,你在Mounted on 这一列数据中可以找到你的U盘或新添加的硬盘的挂载路径.