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

1840: Jack Straws 分享至QQ空间

Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte
Total Submit: 154            Accepted:119

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

Source

East Central North America 1994

我的代码不优秀啊,卡不到0ms,但是这个思想还是挺好的,记录下吧

#include<stdio.h>
#include<algorithm>
using namespace std;
int fa[13],n,a,b;
struct Point
{
    int x1,x2,y1,y2;
    Point(int x1=0,int x2=0,int y1=0,int y2=0):x1(x1),x2(x2),y1(y1),y2(y2){}
    void read()
    {
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    }
} p[13];
int find(int x)
{
    return x==fa[x]?x:fa[x]=find(fa[x]);
}
int cross(int x1,int y1,int x2,int y2)
{
    return x1*y2-x2*y1;
}
int la(Point A,Point B)
{
    if(max(A.x1,A.x2)<min(B.x1,B.x2)||max(A.y1,A.y2)<min(B.y1,B.y2)||max(B.x1,B.x2)<min(A.x1,A.x2)||max(B.y1,B.y2)<min(A.y1,A.y2)) return 0;
    int a=cross(A.x2-A.x1,A.y2-A.y1,B.x1-A.x1,B.y1-A.y1)*cross(A.x2-A.x1,A.y2-A.y1,B.x2-A.x1,B.y2-A.y1),b=cross(B.x2-B.x1,B.y2-B.y1,A.x1-B.x1,A.y1-B.y1)*cross(B.x2-B.x1,B.y2-B.y1,A.x2-B.x1,A.y2-B.y1);
    if(a<=0&&b<=0)return 1;
    return 0;
}
int main()
{
    while(scanf("%d",&n),n)
    {
        for(int i=1; i<=n; i++)
            fa[i]=i,p[i].read();
        for(int i=1; i<n; i++)
            for(int j=i+1; j<=n; j++)
                if(la(p[i],p[j]))
                {
                     a=find(i),b=find(j);
                     if(a!=b)fa[a]=b;
                }
          while(scanf("%d%d",&a,&b),a||b)
          {
              a=find(a),b=find(b);
              if(a!=b)printf("NOT ");
              printf("CONNECTED\n");
          }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/BobHuang/p/8094490.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值