• TZOJ 1840 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 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条线的两个端点坐标,有m个询问,两条线是否连通

    题解

    首先判断任意两条线是否相交,再把相交的加入并查集

    代码

     1 #include<stdio.h>
     2 #include<algorithm>
     3 using namespace std;
     4 
     5 int f[105];
     6 int Find(int x)
     7 {
     8     int r=x;
     9     while(f[r]!=r)
    10         r=f[r];
    11     int i=x,j;
    12     while(i!=r)
    13     {
    14         j=f[i];
    15         f[i]=r;
    16         i=j;
    17     }
    18     return r;
    19 }
    20 void join(int x,int y)
    21 {
    22     int a,b;
    23     a=Find(x);
    24     b=Find(y);
    25     if(a!=b)
    26         f[a]=b;
    27 }
    28 void init()
    29 {
    30     for(int i=0;i<=100;i++)
    31         f[i]=i;
    32 }
    33 
    34 struct point{double x,y;};
    35 bool judge(point a,point b,point c,point d)
    36 {
    37     //用矩形是否重叠快速排斥(共线能通过跨立不行的要排掉)
    38     if(!(min(a.x,b.x)<=max(c.x,d.x)&&min(c.y,d.y)<=max(a.y,b.y)
    39         &&min(c.x,d.x)<=max(a.x,b.x)&&min(a.y,b.y)<=max(c.y,d.y)))
    40         return false;
    41     //跨立
    42     double u,v,w,z;
    43     u=(c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);//u为正说明cb在ab的顺时针
    44     v=(d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y);//db和ab
    45     w=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);//ad和cd
    46     z=(b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y);//bd和cd
    47     return (u*v<=0.00000001&&w*z<=0.00000001);
    48 }
    49 
    50 struct line
    51 {
    52     point p1,p2;
    53 }l[20];
    54 int main()
    55 {
    56     int n;
    57     while(scanf("%d",&n)!=EOF,n)
    58     {
    59         init();
    60         for(int i=1;i<=n;i++)
    61         {
    62             scanf("%lf%lf%lf%lf",&l[i].p1.x,&l[i].p1.y,&l[i].p2.x,&l[i].p2.y);
    63         }
    64         for(int i=1;i<=n;i++)
    65         {
    66             for(int j=1;j<=n;j++)
    67             {
    68                 if(i==j)continue;
    69                 if(judge(l[i].p1,l[i].p2,l[j].p1,l[j].p2))
    70                     join(i,j);
    71             }
    72         }
    73         int a,b;
    74         while(scanf("%d%d",&a,&b)!=EOF,a||b)
    75         {
    76             a=Find(a);
    77             b=Find(b);
    78             if(a==b)
    79                 printf("CONNECTED
    ");
    80             else
    81                 printf("NOT CONNECTED
    ");
    82         }
    83     }
    84     return 0;
    85 }
  • 相关阅读:
    Qt 错误汇集贴
    转:Qt编写串口通信程序全程图文讲解
    转:QT 的点点滴滴 错误总结
    转:Qt项目中遇到的一些小问题汇总
    转:AM335X 启动流程
    基于Xilinx Zynq的计算处理平台
    基于英伟达Jetson TX1的GPU处理平台
    基于6U VPX的 SRIO 接口, 和PCIe 接口的msata 固态存储卡
    国芯网 邀请国产芯片原厂入驻商城
    295-Xilinx Kintex-7 X7K325T的半高PCIe x4双路万兆光纤收发卡
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/8417220.html
Copyright © 2020-2023  润新知