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 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根木棍,木棍i的两端的坐标分别是(pix,piy)和(qix,qiy)。给定有限对木棍,请判断每对木棍是否相连。当两根木棍之间有公共点时,就认为他们是相连的。通过相连的木棍间接的连在一起的两根木棍也认为是相连的。

解题思路
本题可以先判断每两根木棍是否直接相连,然后进行一次Floyd-Warshall算法判断他们是否间接相连。
判断直接相连即判断两线段是否有交点。
这可以分为两种情况讨论。
一:两线段平行。这是只需判断一条线段的一个顶点是否在另一条线段上。
二:两线段不平行。这时先求出两线段所在的直线的交点,然后判断该交点是否在线段上。
注:
在几何问题中,运用向量的內积和外积进行计算非常方便。对于二维向量p1(x1,y1),p2(x2,y2),我们定义內积p1.p2=x1y1+x2y2(一般用来判断向量是否同向),外积p1Xp2=x1y2-x2y1。
要判断点q是否在线段p1-p2上,先利用外积(p1-q)X(p2-q)=0来判断是否在直线上,再利用內积(p1-q).(p2-q)<=0判断q是否在p1-p2之间。而要求两直线的交点,通过变量t将直线p1-p2上的点表示为p1+t(p2-p1),交点又在直线q1-q2上,所以有(q2-q1)X(p1+t(p2-p1)-q1)=0,转化可得t=(p1+((q2-q1)X(q1-p1)/(q2-q1)X(p2-p1))(p2-p1))
代码如下

#include<iostream>
#include<memory.h>
#include<cmath>
using namespace std;
double EPS=1e-10;
int n,m;
struct pp
{
    double x;
    double y;
};
pp p[15];
pp q[15];
bool g[15][15];
double add(double a,double b)//考虑误差的加法运算
{
    if(abs(a+b)<EPS*(abs(a)+abs(b)))
    return 0;
    else
    return a+b;
}
double dot(pp a1,pp b1,pp a2,pp b2)//內积
{
    double x1,y1,x2,y2;
    x1=a1.x-b1.x;
    y1=a1.y-b1.y;
    x2=a2.x-b2.x;
    y2=a2.y-b2.y;
    return add(x1*x2,y2*y1);
}
double det(pp a1,pp b1,pp a2,pp b2)//外积
{
    double x1,y1,x2,y2;
    x1=a1.x-b1.x;
    y1=a1.y-b1.y;
    x2=a2.x-b2.x;
    y2=a2.y-b2.y;
    return add(x1*y2,-x2*y1);
}

pp intersection(pp a1,pp b1,pp a2,pp b2)//求交点
{
    double x1,y1,x2,y2;
    double t;
    pp a,b,c,temp;
    a.x=b2.x-a2.x;
    a.y=b2.y-a2.y;
    b.x=a2.x-a1.x;
    b.y=a2.y-a1.y;
    c.x=b1.x-a1.x;
    c.y=b1.y-a1.y;
    t=add(a.x*b.y,-a.y*b.x)/add(a.x*c.y,-a.y*c.x);
    temp.x=a1.x+t*add(b1.x,-a1.x);
    temp.y=a1.y+t*add(b1.y,-a1.y);

    return temp;
}
bool on_set(pp a,pp b,pp r)//判断某点r是否在某线a-b上
{
    if((det(a,r,b,r)==0)&&(dot(a,r,b,r)<=0))
    return true;
    else
    return false;
}
void solve()
{
    for(int i=1;i<=n;i++)
    {
        g[i][i]=true;

        for(int j=i+1;j<=n;j++)
        {
            if(det(p[i],q[i],p[j],q[j])==0)
            {
                g[i][j]=g[j][i]=on_set(p[i],q[i],p[j])||on_set(p[i],q[i],q[j])||on_set(p[j],q[j],p[i])||on_set(p[j],q[j],q[i]);
            }
            else
            {
                pp r=intersection(p[i],q[i],p[j],q[j]);
                g[i][j]=g[j][i]=on_set(p[i],q[i],r)&&on_set(p[j],q[j],r);
            }
        }
    }


}

int main()
{
    int i,j,k,x,y;
    while(cin>>n)
    {
        if(n==0)
        break;

        memset(g,false,sizeof(g));
        for(i=1;i<=n;i++)
        cin>>p[i].x>>p[i].y>>q[i].x>>q[i].y;

        solve();

        //Floyd-Warshall算法求间接相连
        for(k=1;k<=n;k++)
        for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
        if(g[i][k]&&g[k][j])
        g[i][j]=true;

        while(cin>>x>>y)
        {
            if((x==0)&&(y==0))
            break;

            if(g[x][y])
            cout<<"CONNECTED"<<endl;
            else
            cout<<"NOT CONNECTED"<<endl;
        }

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值