POJ

来源:互联网 发布:在jsp中写java代码 编辑:程序博客网 时间:2024/06/05 02:51

问题描述

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

71 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 020 2 0 00 0 0 11 12 21 20 00

Sample Output

CONNECTED NOT CONNECTED CONNECTED CONNECTED NOT CONNECTED CONNECTEDCONNECTEDCONNECTEDCONNECTED

分析:

这个问题我第一次看的时候以为就是判断两条线段是否相交,直接套了一下模板,后来没a过去,再看看按照直接判断线段相交,样例有一个是错的,后来再看看题目——but also two straws can be connected indirectly via other connected straws,原来可以不直接相交,通过其他相交也可以哦。
又意识到不仔细读题的害处了。

ok,那再加一个并查集就行了。如果两条线段的爹是同一个爹,那么这两条线段就是直接或间接相交了。


代码如下:

#include<cstdio>#include<algorithm>using namespace std;struct Node{    int x, y;};struct Line{    Node a, b;}line[20];int n;int par[20];/*叉乘*/int Direction(Node a, Node b, Node c){    return (c.x-a.x) * (b.y-a.y) - (b.x-a.x) * (c.y - a.y);}/*边界情况*/bool OnSegment(Node a, Node b, Node c){     if(min(a.x,b.x) <= c.x && c.x <= max(a.x,b.x) && min(a.y, b.y) <= c.y && c.y <= max(a.y, b.y))    {        return true;    }    else return false;}/*判断线段相交*/bool SegmentIntersect(Node p1, Node p2, Node p3, Node p4){    int d1 = Direction(p3, p4, p1);    int d2 = Direction(p3, p4, p2);    int d3 = Direction(p1, p2, p3);    int d4 = Direction(p1, p2, p4);    if(((d1<0&&d2>0) || (d1>0&&d2<0)) && ((d3<0&&d4>0) || (d3>0&&d4<0)))             return true;    else if(d1==0 && OnSegment(p3, p4, p1))        return true;    else if(d2==0 && OnSegment(p3, p4, p2))        return true;    else if(d3==0 && OnSegment(p1, p2, p3))        return true;    else if(d4==0 && OnSegment(p1, p2, p4))        return true;    else return false;}/*初始化*/void Init(){    for(int i=0; i<n; i++)    {        par[i] = i;    }    return;}/*找爹*/int find(int x){    int r = x;    while(r!=par[r])    {        r = par[r];    }    return r;}/*判断是不是同一个爹*/bool same(int x, int y){    return find(x) == find(y);}/*合并集合*/void unite(int x, int y){    if(!same(x, y))    {        par[find(x)] = find(y);    }    return;}int main(){    while(scanf("%d",&n)!=EOF&&n)    {        Init();        for(int i=1; i<=n; i++)        {            scanf("%d%d%d%d",&line[i].a.x, &line[i].a.y, &line[i].b.x, &line[i].b.y);        }        for(int i=1; i<=n; i++)        {            for(int j=i+1; j<=n; j++)            {                if(SegmentIntersect(line[i].a, line[i].b, line[j].a, line[j].b) && find(i)!=find(j))                {                    unite(i, j);//如果两条线段的爹不是同一个爹,并且直接相交,那么合并                }            }        }        int a, b;        while(scanf("%d%d",&a,&b)!=EOF)        {            if(a==0 && b==0) break;            if(find(a) == find(b))            {                printf("CONNECTED\n");            }else{                printf("NOT CONNECTED\n");            }        }    }    return 0;}
0 0