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. Django学习笔记(http协议与django安装)

    Django入门 HTTP协议 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于万维网(WWW:World Wide Web )服务器与本地浏览器 ...

  2. notepad++最详情汇总

    1.安装nodepad++ 2.sitting-转换中文语言 3.view-设置自动换行 4.安装格式化插件----https://github.com/bruderstein/nppPluginMa ...

  3. hdu3555数位dp基础

    /* dp[i][0|1|2]:没有49的个数|最高位是9,没有49的个数|有49的个数 dp[i][0]=10*dp[i-1][0]-dp[i-1][1] dp[i][1]=dp[i-1][0] d ...

  4. tensorflow变量-【老鱼学tensorflow】

    在程序中定义变量很简单,只要定义一个变量名就可以,但是tensorflow有点类似在另外一个世界,因此需要通过当前的世界中跟tensorlfow的世界中进行通讯,来告诉tensorflow的世界中定义 ...

  5. 微信小程序--家庭记账本开发--03

    组件.标签以及模板的使用 在一个微信小程序中,需要用到大量的组件,一些页面的设计也需要模板,在自己所学课程中,对于一些组件.标签模板的使用有了初步的了解. 1.组件 组件是数据和方法的简单封装,对于微 ...

  6. pandas 必背函数操作

    1.五个常用属性 index,columns,shape,values,dtypes2.常用函数:set_index,reset_index,del df['column_name'],pd.read ...

  7. STL复习之 map & vector --- disney HDU 2142

    题目链接: https://vjudge.net/problem/40913/origin 大致题意: 这是一道纯模拟题,不多说了. 思路: map模拟,vector辅助 其中用了map的函数: er ...

  8. 创建phpinfo(PHP探针)查看自己服务器空间php详细信息

    创建phpinfo(PHP探针)查看自己服务器空间php详细信息 <?phpphpinfo();?> 保存,然后更改文件名为phpinfo.php 放到你域名根目录,然后访问:http:/ ...

  9. python ironicclient源码分析

    ironicclient是一个cli工具,用来和用户交互的. 首先写一个简单的例子,获取ironic所有的node节点: from ironicclient import client if __na ...

  10. web测试注意点

    关于网页测试我们需要注意的地方有: 1.每次测试之前都需要代码更新.清理缓存,测试数据使用新数据. 2.各模块的信息归类是否正确.比如进入一级栏目或二级栏目的列表页,查看左侧栏目名称,右侧文章标题及内 ...