Jack Straws(POJ 1127)

  • 原题如下:
    Jack Straws
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 5555   Accepted: 2536

    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
  • 题解:问题的关键是判断线段是否相交,然后就可以建图进行连接性判断。首先会想到计算两直线的交点然后判断交点是否在线段上的方法,这样问题就变成如何判断点是否在线段上以及如何求两直线的交点。在几何问题中,运用向量的内积和外积进行计算是非常方便的。对于二维向量p1=(x1,y1)和p2=(x2,y2),定义内积p1·p2=x1x2+y1y2,外积p1×p2=x1y2-y1x2。要判断点q是否在线段p1-p2上,只要先根据外积(p1-q)×(p2-q)是否等于0来判断点q是否在直线p1-p2上,再利用内积(p1-q)·(p2-q)是否小于等于0来判断点q是否落在p1-p2之间。而要求两直线的交点,通过变量t将直线p1-p2上的点表示为p1+t(p2-p1),交点又在直线q1-q2上,所以有:(q2-q1)×(p1+t(p2-p1)-q1)=0,于是可以利用下式求得t的值:

    但是,使用这个方法还要注意边界情况,如果两条线段是平行的,也有可能有公共点,可以通过检查端点是否在另一条线段上来判断。
  • 代码:
      1 #include <cstdio>
      2 #include <cmath>
      3 #include <algorithm>
      4 
      5 using namespace std;
      6 
      7 const double EPS=1e-10;
      8 
      9 double add(double a, double b)
     10 {
     11     if (fabs(a+b)<EPS*(fabs(a)+fabs(b))) return 0;
     12     return a+b;
     13 }
     14 
     15 struct P
     16 {
     17     double x,y;
     18     P(){};
     19     P(double x, double y):x(x),y(y){}
     20     P operator + (P p)
     21     {
     22         return P(add(x, p.x), add(y, p.y));
     23     }
     24     P operator - (P p)
     25     {
     26         return P(add(x, -p.x), add(y, -p.y));
     27     }
     28     P operator * (double d)
     29     {
     30         return P(x*d, y*d);
     31     }
     32     double dot(P p)
     33     {
     34         return add(x*p.x, y*p.y);
     35     }
     36     double det(P p)
     37     {
     38         return add(x*p.y, -y*p.x);
     39     }
     40 };
     41 
     42 bool on_seg(P p1, P p2, P q)
     43 {
     44     return (p1-q).det(p2-q)==0 && (p1-q).dot(p2-q)<=0;
     45 }
     46 
     47 P intersection(P p1, P p2, P q1, P q2)
     48 {
     49     return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
     50 }
     51 
     52 const int MAX_N=15;
     53 const int MAX_M=2000;
     54 int n;
     55 P p[MAX_N], q[MAX_N];
     56 int m;
     57 int a[MAX_M], b[MAX_M];
     58 bool g[MAX_N][MAX_N]; 
     59 
     60 int main()
     61 {
     62     while (~scanf("%d", &n) && n)
     63     {
     64         fill(g[0], g[0]+sizeof(bool)*15*15, false);
     65         for (int i=0; i<n; i++)
     66         {
     67             scanf("%lf %lf %lf %lf", &p[i].x, &p[i].y, &q[i].x, &q[i].y);
     68         }
     69         for (int i=0;;i++)
     70         {
     71             scanf("%d %d",&a[i], &b[i]);
     72             if (a[i]==0 && b[i]==0)
     73             {
     74                 m=i;
     75                 break;
     76             }
     77         }
     78         for (int i=0; i<n; i++)
     79         {
     80             g[i][i]=true;
     81             for (int j=0; j<i; j++)
     82             {
     83                 if ((p[i]-q[i]).det(p[j]-q[j])==0)
     84                 {
     85                     g[i][j]=g[j][i]=on_seg(p[i], q[i], p[j])
     86                                  || on_seg(p[i], q[i], q[j])
     87                                  || on_seg(p[j], q[j], p[i])
     88                                  || on_seg(p[j], q[j], q[i]);
     89                 }
     90                 else
     91                 {
     92                     P r=intersection(p[i], q[i], p[j], q[j]);
     93                     g[i][j]=g[j][i]=on_seg(p[i], q[i], r) && on_seg(p[j], q[j], r);
     94                 }
     95             }
     96         }
     97         for (int k=0; k<n; k++)
     98             for (int i=0; i<n; i++)
     99                 for (int j=0; j<n; j++)
    100                 {
    101                     g[i][j] |= g[i][k]&&g[k][j]; 
    102                 } 
    103         for (int i=0; i<m; i++)
    104         {
    105             puts(g[a[i]-1][b[i]-1] ? "CONNECTED" : "NOT CONNECTED");
    106         }
    107     }
    108 }
原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9791117.html