POJ1127

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

题目意思:就是给你很多坐标系上的线段,判断一对线段是否有交点,通过其他线段连接在一起的也算。这种间接连接的,通过Floyd-Warshall算法进行传递闭包运算。

 这里判断两个线段是够有交点,有个原理:https://blog.csdn.net/qq826309057/article/details/70942061

解释一下变量的含义:

p[]:记录线段的顶点,和q[]配合使用,p[i]和q[i]是指线段i的2个端点坐标。

q[]:

G[][]:G[i][j]=1表示,线段i和线段j相交,否则不相交。

crossP就是计算2个向量的外积,三个参数是3个坐标点p,q1,q2

 

 两个线段相交,根据上述的原理

这两个结果会<=0 ,0是临界情况,如下

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;       
#define mem(a,b) memset(a,b,sizeof(a))    
#define N 14
struct point{
    double x,y;
};
point p[N],q[N];
int G[N][N];
double crossP(point p,point q1,point q2)
{
    double x1=p.x-q1.x,y1=p.y-q1.y,x2=q2.x-q1.x,y2=q2.y-q1.y;
    return x1*y2-y1*x2;
}
bool isIntersection(point p1,point p2,point q1,point q2)
{
    if(max(p1.x,p2.x)<min(q1.x,q2.x)||max(p1.y,p2.y)<min(q1.y,q2.y)||max(q1.x,q2.x)<min(p1.x,p2.x)||max(q1.y,q2.y)<min(p1.y,p2.y))
    {
        return false;
    }
    if(crossP(p1,q1,q2)*crossP(p2,q1,q2)>0||crossP(q1,p1,p2)*crossP(q2,p1,p2)>0)
    {
        return false;
    }
    return true;
}
int main(){
    int n,a,b;
    while(cin>>n,n){
        for(int i=1;i<=n;i++)
        {
            cin>>p[i].x>>p[i].y>>q[i].x>>q[i].y;
        }
        mem(G,0);
        for(int i=1;i<=n;i++)
        {
            G[i][i]=1;
            for(int j=1;j<i;j++)
            {
                if(isIntersection(p[i],q[i],p[j],q[j]))
                {
                    G[i][j]=G[j][i]=1;
                }
            }
        }
        for(int k=1;k<=n;k++)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    G[i][j]|=G[i][k]&&G[k][j];
                }
            }
        }
        while(cin>>a>>b,a+b)
        {
            if(G[a][b])
            {
                cout<<"CONNECTED\n";
            }
            else
            {
                cout<<"NOT CONNECTED\n";
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值