zoukankan      html  css  js  c++  java
  • Jack Straws(poj 1127) 两直线是否相交模板

     

     

    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


    
    
    给你 n 个木棍, 每根木棍 4 个坐标, 给你两个编号, 问这两个编号的木棍是否相交(可以间接相交) 




    
    
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
     
    using namespace std;
     
    #define N 20
    const double eps=1e-10;
     
    struct Point
    {
        int x, y;
    };
     
    struct node
    {
        Point a;
        Point b;
    }P[N];
     
    int G[N][N], n;
     
    /**--------- 判断两线段相交 模板 ------------**/ 
    int Judge(int x, int y)
    {
         Point a, b, c, d;
         a = P[x].a, b = P[x].b;
         c = P[y].a, d = P[y].b;
         if ( min(a.x, b.x) > max(c.x, d.x) ||
              min(a.y, b.y) > max(c.y, d.y) ||
              min(c.x, d.x) > max(a.x, b.x) ||
              min(c.y, d.y) > max(a.y, b.y) ) return 0;
        double h, i, j, k;
        h = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
        i = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x);
        j = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x);
        k = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x);
        return h * i <= eps && j * k <= eps;
    }
     
    void Slove()
    {
        int i, j, k;
     
        for(i=1; i<=n; i++)
        for(j=i+1; j<=n; j++)
        {
            if(Judge(i, j))
                G[i][j] = G[j][i] = 1;
        }
     
        for(k=1; k<=n; k++)
        for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
        {
            if(G[i][k] && G[k][j])
                G[i][j] = 1;
        }
    }
     
    int main()
    {
        while(scanf("%d", &n), n)
        {
            int i, u, v;
     
            for(i=1; i<=n; i++)
                scanf("%d%d%d%d", &P[i].a.x, &P[i].a.y, &P[i].b.x, &P[i].b.y);
     
            memset(G, 0, sizeof(G));
            Slove();
            while(scanf("%d%d", &u, &v), u+v)
            {
                if(G[u][v] || u==v) printf("CONNECTED
    ");
                else        printf("NOT CONNECTED 
    ");
            }
        }
     
        return 0;
    }

     

     
    勿忘初心
  • 相关阅读:
    【小白的CFD之旅】14 实例反思
    【小白的CFD之旅】13 敲门实例【续3】
    【小白的CFD之旅】12 敲门实例【续2】
    【小白的CFD之旅】11 敲门实例【续】
    【小白的CFD之旅】10 敲门实例
    【小白的CFD之旅】09 初识FLUENT
    【小白的CFD之旅】08 CFD速成之道
    【小白的CFD之旅】07 CFD常识
    【小白的CFD之旅】06 流体力学基础
    软件工程-构建之法 理解C#一小段程序
  • 原文地址:https://www.cnblogs.com/YY56/p/5383097.html
Copyright © 2011-2022 走看看