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. python回归分析五部曲

    Python回归分析五部曲(一)—简单线性回归 https://blog.csdn.net/jacky_zhuyuanlu/article/details/78878405?ref=myread Py ...

  2. 【深度学习】吴恩达网易公开课练习(class2 week1)

    权重初始化 参考资料: 知乎 CSDN 权重初始化不能全部为0,不能都是同一个值.原因是,如果所有的初始权重是相同的,那么根据前向和反向传播公式,之后每一个权重的迭代过程也是完全相同的.结果就是,无论 ...

  3. 微信H5支付证书过滤

    在对接微信支付,退款的时候,遇到 Caused by: java.lang.RuntimeException: java.io.IOException: DerInputStream.getLengt ...

  4. IDEA手动创建JFinal项目

    http://www.jfinal.com/share/674 https://www.oschina.net/question/265150_110300

  5. WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!问题

    ➜ web_develop git:(master) ✗ ssh root@172.16.146.143@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  6. Spring AOP概念理解

    1.我所知道的aop 初看aop,上来就是一大堆术语,而且还有个拉风的名字,面向切面编程,都说是OOP的一种有益补充等等.一下子让你不知所措,心想着:怪不得很多人都和我说aop多难多难.当我看进去以后 ...

  7. 【Android】activity-alias的使用

    activity-alias是android里为了重复使用Activity而设计的. 当在Activity的onCreate()方法里,执行getIntent().getComponent().get ...

  8. 昨天开始使用lr controller 已停止工作问题

    其实看到这个,只能看日志 看到日志也是无能为力 然后只能尝试修复,但是无法解决,最后通过重装系统,问题解决

  9. Monolog手册参考

    https://api.kdyby.org/namespace-Monolog.Handler.html

  10. Python 求点到直线的垂足

    Python 求点到直线的垂足 在已知一个点,和一条已知两个点的直线的情况下 运算公式参考链接:https://www.cnblogs.com/mazhenyu/p/3508735.html def ...