1840: Jack Straws

时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte
总提交:
168
          
测试通过:129

描述

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 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.

输出

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.

样例输入

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

样例输出

CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
CONNECTED
CONNECTED

题目来源

East Central North America 1994

解题思路:判断2条直线是不是相交+并查集

 #include <bits/stdc++.h>  //1840: Jack Straws
using namespace std;
struct Point{
int x1,y1,x2,y2;
}A[];
int arr[]; struct Node{
int x,y;
};
bool judge(Point p1,Point p2){
if(max(p1.x1,p1.x2)<min(p2.x1,p2.x2)||max(p1.y1,p1.y2)<min(p2.y1,p2.y2)||min(p1.x1,p1.x2)>max(p2.x1,p2.x2)||min(p1.y1,p1.y2)>max(p2.y1,p2.y2))
return false;
return true;
} int cultilate(Point p1,Point p2){
Node AB,AC,AD,CD,CB,CA;
AB.x=p1.x2-p1.x1,AB.y=p1.y2-p1.y1;
AC.x=p2.x1-p1.x1,AC.y=p2.y1-p1.y1;
AD.x=p2.x2-p1.x1,AD.y=p2.y2-p1.y1;
CD.x=p2.x2-p2.x1,CD.y=p2.y2-p2.y1;
CB.x=p1.x2-p2.x1,CB.y=p1.y2-p2.y1;
CA.x=p1.x1-p2.x1,CA.y=p1.y1-p2.y1;
if((AB.x*AC.y-AB.y*AC.x)*(AB.x*AD.y-AB.y*AD.x)<=&&(CD.x*CB.y-CD.y*CB.x)*(CD.x*CA.y-CD.y*CA.x)<=)
return ;
return ;
} int find_root(int x){
return arr[x]==x?x:arr[x]=find_root(arr[x]);
} int unionset(int x,int y){
int xx=find_root(x),yy=find_root(y);
if(xx!=yy){
if(xx>yy){
arr[yy]=xx;
}
else arr[xx]=yy;
}
} int main()
{
ios::sync_with_stdio(false);
int n;
while(cin>>n&&n!=){
for(int i=;i<=;i++){
arr[i]=i;
}
for(int i=;i<=n;i++){
cin>>A[i].x1>>A[i].y1>>A[i].x2>>A[i].y2;
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
bool flag;
flag=judge(A[i],A[j]);
if(flag==true){
int zhi;
zhi=cultilate(A[i],A[j]);
if(zhi==){ //connect
unionset(i,j);
}
}
}
}
int d1,d2;
while(cin>>d1>>d2&&d1!=&&d2!=){
if(find_root(d1)==find_root(d2)){
cout << "CONNECTED" << endl;
}
else cout << "NOT CONNECTED" << endl;
}
}
return ;
}

1840: Jack Straws的更多相关文章

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

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

  2. TOJ 1840 Jack Straws

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

  3. TOJ1840: Jack Straws 判断两线段相交+并查集

    1840: Jack Straws  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal Submit: 1 ...

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

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

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

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

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

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

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

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

  8. Jack Straws POJ - 1127 (几何计算)

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

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

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

随机推荐

  1. 【原创】大叔问题定位分享(30)mesos agent启动失败:Failed to perform recovery: Incompatible agent info detected

    mesos agent启动失败,报错如下: Feb 15 22:03:18 server1.bj mesos-slave[1190]: E0215 22:03:18.622994 1192 slave ...

  2. 【原创】大叔问题定位分享(11)Spark中对大表子查询加limit为什么会报Broadcast超时错误

    当两个表需要join时,如果一个是大表,一个是小表,正常的map-reduce流程需要shuffle,这会导致大表数据在节点间网络传输,常见的优化方式是将小表读到内存中并广播到大表处理,避免shuff ...

  3. psvm中String [] args 理解

    初学Java时,只是按部就班地写:public static void main(String [] args){} 却一直不知道参数说明作用,原来: 这个参数是默认从控制台中传递参数的. 运行这段程 ...

  4. [原创]Xilinx工具关联UEStudio

    UE安装目录如下: C:\Program Files (x86)\IDM Computer Solutions\UEStudio\UEStudio.exe 对于ISE工具,在Editor -> ...

  5. Win下必备神器之Cmder

    诚言,对于开发码字者,Mac和Linux果断要比Windows更贴心;但只要折腾下,Windows下也是有不少利器的.之前就有在Windows下效率必备软件一文中对此做了下记载:其虽没oh-my-zs ...

  6. React实现局部刷新

    [项目结构] 流程: 入口文件 -> 路由 -> layout -> Analysi/Monitor/Workspace 1.入口文件 -> src/index.js 2.组件 ...

  7. leetcode刷题第二天<两数相加>

    题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...

  8. H5实现全屏与F11全屏

    最近做项目用到全屏,现总结一下全屏: 1.局部全屏:H5全屏和F11有区别,在这种情况下判断全屏只需要通过H5全屏属性,无论全屏后有无滚动条都可判断. /** * [isFullscreen 判断浏览 ...

  9. http连接基础类,负责底层的http通信

    /// <summary> /// http连接基础类,负责底层的http通信 /// </summary> public class HttpService { public ...

  10. SQLyog 最新版本12.5-64bit 完美破解,亲测可用!

    声明:本文只是提供一个网络上找到的针对12.5版本的注册码使用方式做一个说明,不建议企业用户破解,有条件的还是希望大家购买原版.当然个人学习用的但又不想购买原版的,这里只是提供个途径,请勿用做商业用途 ...