zoukankan      html  css  js  c++  java
  • poj 3304 直线与线段相交

    Segments
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12161   Accepted: 3847

    Description

    Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common.

    Input

    Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing a positive integer n ≤ 100 showing the number of segments. After that, n lines containing four real numbers x1 y1 x2 y2 follow, in which (x1y1) and (x2y2) are the coordinates of the two endpoints for one of the segments.

    Output

    For each test case, your program must output "Yes!", if a line with desired property exists and must output "No!" otherwise. You must assume that two floating point numbers a and b are equal if |a - b| < 10-8.

    Sample Input

    3
    2
    1.0 2.0 3.0 4.0
    4.0 5.0 6.0 7.0
    3
    0.0 0.0 0.0 1.0
    0.0 1.0 0.0 2.0
    1.0 1.0 2.0 1.0
    3
    0.0 0.0 0.0 1.0
    0.0 2.0 0.0 3.0
    1.0 1.0 2.0 1.0

    Sample Output

    Yes!
    Yes!
    No!
    /*
    poj 3304 直线与线段相交
    
    给你n条线段,确定是否存在一条直线,它们的投影到上面时有公共点
    
    如果存在一个这样的直线,那么说明所有的线段能和一条直线相交
    对这条直线进行一定的旋转,必定与所有直线的端点中的至少两个相交
    所以枚举所有的端点进行判断即可
    //注意判断相同点
    
    hhh-2016-05-04 20:48:26
    */
    #include <iostream>
    #include <vector>
    #include <cstring>
    #include <string>
    #include <cstdio>
    #include <queue>
    #include <cmath>
    #include <algorithm>
    #include <functional>
    #include <map>
    using namespace std;
    #define lson  (i<<1)
    #define rson  ((i<<1)|1)
    typedef long long ll;
    const int  maxn = 40010;
    double eps = 1e-8;
    int tot;
    int n,m;
    double x1,x2,y1,y2;
    
    int sgn(double x)
    {
        if(fabs(x) < eps) return 0;
        if(x < 0)
            return -1;
        else
            return 1;
    }
    
    struct Point
    {
        double x,y;
        Point() {}
        Point(double _x,double _y)
        {
            x = _x,y = _y;
        }
        Point operator -(const Point &b)const
        {
            return Point(x-b.x,y-b.y);
        }
        double operator ^(const Point &b)const
        {
            return x*b.y-y*b.x;
        }
    };
    
    struct Line
    {
        Point s,t;
        Line() {}
        Line(Point _s,Point _t)
        {
            s = _s;
            t = _t;
        }
    };
    int tans[maxn];
    Line line[maxn];
    Point po[maxn];
    Point p;
    
    bool seg_inter_line(Line l1,Line l2)
    {
        return sgn((l2.s-l1.t) ^ (l1.s-l1.t))*sgn((l2.t-l1.t)^(l1.s-l1.t)) <= 0;
    }
    
    bool cal(Line tl)
    {
        for(int i = 0;i < n;i++)
        {
            if(!seg_inter_line(tl,line[i]))
            {
                return false;
            }
        }
        return true;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            int flag = 0;
            tot = 0;
            if(n < 3)
                flag = 1;
            for(int i = 0; i < n; i++)
            {
                scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
                po[tot++] = Point(x1,y1);
                po[tot++] = Point(x2,y2);
                line[i] = Line(po[tot-1],po[tot-2]);
            }
            for(int i = 0; i < tot && !flag; i++)
            {
                for(int j = i+1; !flag && j < tot; j++)
                {
                    if(po[i].x == po[j].x && po[i].y == po[j].y)
                        continue;
                    if(cal(Line(po[i],po[j])))
                    {
                        flag = 1;
                        break;
                    }
                }
            }
            if(flag)
                printf("Yes!
    ");
            else
                printf("No!
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    BZOJ1036 [ZJOI2008]树的统计Count 树链剖分
    SPOJ-QTREE Query on a tree 树链剖分
    BZOJ3224 洛谷3369 Tyvj 1728 普通平衡树 splay
    BZOJ1008 [HNOI2008]越狱 快速幂
    HDU4686 Arc of Dream 矩阵
    POJ2065 SETI 高斯消元
    POJ1487 Single-Player Games 高斯消元
    HDU3306 Another kind of Fibonacci 矩阵
    Hadoop基本操作
    Hadoop命令大全
  • 原文地址:https://www.cnblogs.com/Przz/p/5510310.html
Copyright © 2011-2022 走看看