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

Jack Straws

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

题目大意:按顺序输入n个线段的两个坐标,然后多组输入判断两个线段是否是连接的(相交即为连接)。

题解:利用计算几何的知识,建立线段,如果有线段相交,就用并查集把它们连在一起,然后判断根节点是不是一个就好啦。比较简单的模板题目。

有问题欢迎━(*`?′*)ノ亻!指出!

#include<cstdio>
#include<algorithm>
using namespace std;
struct node{
    double x,y;
};
struct d
{
    node a;
    node b;
}deline[15];
double cross(node a,node b,node o)
{
    return(a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
}
bool isxj(d u,d v)
{
    return (cross(v.a,u.b,u.a)*cross(u.b,v.b,u.a)>=0)&&
        (cross(u.a,v.b,v.a)*cross(v.b,u.b,v.a)>=0)&&
        (max(u.a.x,u.b.x)>=min(v.a.x,v.b.x))&&
        (max(v.a.x,v.b.x)>=min(u.a.x,u.b.x))&&
        (max(u.a.y,u.b.y)>=min(v.a.y,v.b.y))&&
        (max(v.a.y,v.b.y)>=min(u.a.y,u.b.y));
}
int father[20];
int find(int x)
{
    return x==father[x]?x:father[x]=find(father[x]);
}
void mix(int x,int y)
{
    int xx=find(x),yy=find(y);
    if(xx!=yy)
    {
        father[xx]=yy;
    }
}
int main(){
    int n;
    while(scanf("%d",&n),n)
    {
        for(int i=0;i<20;i++)
        {
            father[i]=i;
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%lf %lf %lf %lf",&deline[i].a.x,&deline[i].a.y,&deline[i].b.x,&deline[i].b.y);
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(isxj(deline[i],deline[j]))
                {
                    mix(i,j);
                }
            }
        }
        int a,b;
        while(scanf("%d%d",&a,&b),a&&b)
        {
            if(find(a)==find(b))
                printf("CONNECTED\n");
            else
                printf("NOT CONNECTED\n");
        }
    }
}

原文地址:https://www.cnblogs.com/Tangent-1231/p/9332811.html

时间: 2024-12-24 13:19:44

poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)的相关文章

You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6837 Accepted Submission(s): 3303 Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. A

hdu 1558 线段相交+并查集

题意:要求相交的线段都要塞进同一个集合里 sol:并查集+判断线段相交即可.n很小所以n^2就可以水过 1 #include <iostream> 2 #include <cmath> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 int f[1010]; 8 char ch; 9 int tmp,n; 10 double X1,X2,Y1,Y2; 11 12 #def

POJ 1127 Jack Straws ( 求直线交点, 判断线段是否相交(含端点) )

题目:传送门 题意: 给你 n 条线段的两个端点, 然后有多次询问, 每次询问, 问你线段 x 和 线段 y 是否相交. 若线段 A 和线段 B 相交且线段 A 和线段 C 相交,那么线段 B 和线段 C 相交.     1 < n < 13 题解: 暴力求线段是否相交, 然后再跑个 Floyd 或者并查集都可以的. #include <iostream> #include <stdio.h> #include <string.h> #include <

POJ 1127 Jack Straws (计算几何)

Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3945   Accepted: 1787 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 witho

poj 1127 Jack Straws 线段相交+并查集

题意: 有n个木棍,给出木棍的两个端点的x,y坐标,判断其中某两个线段是否连通(可通过其他线段连通) #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <map> #include <

POJ 1127 Jack Straws (线段相交)

题意:给定一堆线段,然后有询问,问这两个线段是不是相交,并且如果间接相交也可以. 析:可以用并查集和线段相交来做,也可以用Floyd来做,相交就是一个模板题. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #includ

POJ 1127 Jack Straws 几何基础

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

POJ - 1127 Jack Straws(几何)

题意:桌子上放着n根木棍,已知木棍两端的坐标.给定几对木棍,判断每对木棍是否相连.当两根木棍之间有公共点或可以通过相连的木棍间接的连在一起,则认为是相连的. 分析: 1.若线段i与j平行,且有部分重合,则相连.否则,求直线i与直线j交点,再判断该交点是否在两线段上,以确定是否相连. 2.flod整理一下所有的关系. 3.判断点是否在线段上:(线段i,点t) (1)外积(l[i] - t) × (r[i] - t) = 0, 可判断点x是否在直线i上(两向量叉乘为0,两向量平行) (2)内积(l[

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

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3512   Accepted: 1601 Description In the game of Jack Straws, a number of plastic or wooden "straws" are dumped o