TOJ Jack Straws (并查集+线段相交判断)(附叉积相乘小于0判断法简略证明)

Jack Straws

时间限制(普通/Java):1000MS/10000MS 内存限制:65536KByte

描述

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

 题目大意:桌子上放着n根木棍,给定有限对木棍,请判断每对木棍是否相连。当两根木棍之间有公共点时,就认为他们是相连的。通过相连的木棍间接的连在一起的两根木棍也认为是相连的。

思路:利用并查集来将间接相连的木棍放入一个集合里。

两个直接相连的木棍判断:利用快速排斥实验和跨立实验

快速排斥实验:判断以当前向量为对角线作矩形,若两个矩形不相交直接可以排除这两条线段相交的可能。

跨立实验:利用向量叉积求解。

若两条线段ab,cd相交,则a和b一定在cd两端,cd同理。因此只要证明它们分别在两端即可

向量叉积相乘<0可得点分别在直线两端。(本人没有系统学习过这方面知识错了轻喷QaQ)

因为向量叉积是一个矢量且表示面积(具体证明可以去b站搜索),可得对于如图ac向量由cd顺时针旋转得来,bc由cd逆时针旋转得来。则若定义 s2为正,s1则为负。

则若使得s1*s2 <0可得a,b在cd向量两端。

\underset{ a}{\rightarrow}=(X1 ,Y1)\underset{b}{\rightarrow}=(X2,Y2)

叉积公式:a x b =X1Y2-X2Y1

ac code:

#include<bits/stdc++.h>
using namespace std;
int father[50];
int n, m;
struct edge{
   double x1, y1, x2, y2;
}a[50];
void init()
{
    for (int i =0; i < 50; i++)
        father[i] = i;
}
int findfa(int a)
{ 
    if (a != father[a])father[a] = findfa(father[a]);
    return father[a];
}
double cj(double x1, double y1, double x2, double y2)
{
    return x1 * y2 - x2 * y1;
}
bool check(int x, int y)
{
    if (max(a[x].x1, a[x].x2) < min(a[y].x1, a[y].x2) || max(a[x].y1, a[x].y2) < min(a[y].y1, a[y].y2) || max(a[y].x1, a[y].x2) < min(a[x].x1, a[x].x2) || max(a[y].y1, a[y].y2) < min(a[x].y1, a[x].y2))return 0;
    double e[4];
    double a1 = a[x].x1, a2 = a[x].y1, a3 = a[x].x2, a4 = a[x].y2;
    double q1 = a[y].x1, q2 = a[y].y1, q3 = a[y].x2, q4 = a[y].y2;
    e[0] = cj(q1 - a1, q2 - a2, a3 - a1, a4 - a2);
    e[1] = cj(q3 - a1, q4 - a2, a3 - a1, a4 - a2);
    e[2] = cj(a1 - q1, a2 - q2, q3 - q1, q4 - q2);
    e[3] = cj(a3 - q1, a4 - q2, q3 - q1, q4 - q2);
    if (e[0] * e[1] <= 0.0000001 && e[2] * e[3] <= 0.0000001) {
        return 1;
    }
    return 0;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    while (cin>>n,n!=0)
    {
        init();
        for (int i = 1; i <= n; i++)
            cin >> a[i].x1 >> a[i].y1 >> a[i].x2 >> a[i].y2;

        for (int i = 1; i <= n; i++)
            for (int j = i + 1; j <= n; j++)
                if (check(i, j))
                {
                    int a = findfa(i), b = findfa(j);
                    if(a != b) 
                        father[a] =b;
                }
        int x, y;
        while (cin >> x >> y, x != 0, y != 0)
        {
            if (findfa(x) != findfa(y))cout << "NOT CONNECTED\n";
            else cout << "CONNECTED\n";
        }
    }

    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值