Jack Straws(并差集和判断直线相交问题)

Jack Straws

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3155   Accepted: 1418

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

意解: 并差集加计算几何知识 (注意判断直线共线但不相交的情况)

可以作为判断直线相交的模板...

AC代码:

#include <iostream>
#include <cstdio>

using namespace std;
int set[20];

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[20];

void unit(int n)
{
    for(int i = 1; i <= n; i++) set[i] = i;
}

int find(int x)
{
    return x == set[x] ? x : set[x] = find(set[x]);
}

int cross(int x1, int y1, int x2, int y2)
{
    return x1 * y2 - x2 * y1;
}

int intersection(Point A, Point B) //判断直线相交
{
    int c[4];
    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; //考虑共线不相交的情况,为快速排斥定理
    /*判断两条直线是否相交,即只需判断线是否在另一条线的两端*/
    c[0] = cross(A.x2 - A.x1, A.y2 - A.y1, B.x1 - A.x1, B.y1 - A.y1);
    c[1] = cross(A.x2 - A.x1, A.y2 - A.y1, B.x2 - A.x1, B.y2 - A.y1);
    c[2] = cross(B.x2 - B.x1, B.y2 - B.y1, A.x1 - B.x1, A.y1 - B.y1);
    c[3] = cross(B.x2 - B.x1, B.y2 - B.y1, A.x2 - B.x1, A.y2 - B.y1);
    if(c[0] * c[1] <= 0 && c[2] * c[3] <= 0) return 1; //运用到了向量的叉乘和点乘的知识;
    return 0;
}

int main()
{
    //freopen("in.txt","r",stdin);
    int n;
    while(~scanf("%d",&n) && n)
    {
        for(int i = 1; i <= n; i++)
            p[i].read();
        unit(n);
        for(int i = 1; i <= n; i++)
        {
            for(int j = i + 1; j <= n; j++)
            {
                if(intersection(p[i],p[j]))
                {
                    int a = find(i);
                    int b = find(j);
                    if(a != b) set[a] = b;
                }
            }
        }
        int a,b;
        while(~scanf("%d %d",&a,&b), a | b)
        {
            a = find(a);
            b = find(b);
            if(a == b) puts("CONNECTED");
            else puts("NOT CONNECTED");
        }
    }
    return 0;
}
时间: 2024-10-04 02:46:32

Jack Straws(并差集和判断直线相交问题)的相关文章

POJ 1269 Intersecting Lines(判断直线相交)

题目地址:POJ 1269 直接套模板就可以了...实在不想自己写模板了...写的又臭又长....不过这题需要注意的是要先判断是否有直线垂直X轴的情况. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h>

poj 1556 zoj1721 BellmanFord 最短路+判断直线相交

http://poj.org/problem?id=1556 The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6120   Accepted: 2455 Description You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will a

[poj] 1066 Treasure Hunt || 判断直线相交

原题 在金字塔内有一个宝藏p(x,y),现在要取出这个宝藏. 在金字塔内有许多墙,为了进入宝藏所在的房间必须把墙炸开,但是炸墙只能炸每个房间墙的中点. 求将宝藏运出城堡所需要的最小炸墙数. 判断点和直线相交. 枚举每道墙的两个端点和p的连线这条线段和墙的交点的次数最小值即为需要炸墙的最小次数. [注意当墙数为零时输出1:] #include<cstdio> #include<algorithm> #define N 33 using namespace std; int ans=0

POJ 1269 Intersecting Lines【判断直线相交】

题意:给两条直线,判断相交,重合或者平行 思路:判断重合可以用叉积,平行用斜率,其他情况即为相交. 求交点: 这里也用到叉积的原理.假设交点为p0(x0,y0).则有: (p1-p0)X(p2-p0)=0 (p3-p0)X(p2-p0)=0 展开后即是 (y1-y2)x0+(x2-x1)y0+x1y2-x2y1=0 (y3-y4)x0+(x4-x3)y0+x3y4-x4y3=0 将x0,y0作为变量求解二元一次方程组. 假设有二元一次方程组 a1x+b1y+c1=0; a2x+b2y+c2=0

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 o

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

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

判断线段和直线相交 POJ 3304

1 // 判断线段和直线相交 POJ 3304 2 // 思路: 3 // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. 4 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 #include <algorithm> 9 #include <map> 10 #include <set> 11 #inclu

POJ 3304 Segments 判断直线和线段相交

POJ 3304  Segments 题意:给定n(n<=100)条线段,问你是否存在这样的一条直线,使得所有线段投影下去后,至少都有一个交点. 思路:对于投影在所求直线上面的相交阴影,我们可以在那里作一条线,那么这条线就和所有线段都至少有一个交点,所以如果有一条直线和所有线段都有交点的话,那么就一定有解. 怎么确定有没直线和所有线段都相交?怎么枚举这样的直线?思路就是固定两个点,这两个点在所有线段上任意取就可以,然后以这两个点作为直线,去判断其他线段即可.为什么呢?因为如果有直线和所有线段都相