zoukankan      html  css  js  c++  java
  • POJ 3304 Segments[直线与线段相交]

    Segments
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13514   Accepted: 4331

    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!

    Source


    题目大意:给出n条线段两个端点的坐标,问所有线段投影到一条直线上,如果这些所有投影至少相交于一点就输出Yes!,否则输出No!。

    暴力枚举线段的交点组成直线然后判相交就行了
    判断线段直线相交:两个叉积不同 (此题要额外判断两个sgn后都为0的情况(会当作同号哦) 就是重合 也算相交)
    注意:
    1.两点之间距离<1e-8是重合的
    2.有可能恰好与一条线段重合
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    using namespace std;
    typedef long long ll;
    const int N=105;
    const double eps=1e-8;
    inline int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
        return x*f;
    }
    inline int sgn(double x){
        if(abs(x)<eps) return 0;
        else return x<0?-1:1;
    }
    struct Vector{
        double x,y;
        Vector(double a=0,double b=0):x(a),y(b){}
        bool operator <(const Vector &a)const{
            return x<a.x||(x==a.x&&y<a.y);
        }
        void print(){
            printf("%lf %lf
    ",x,y);
        }
    };
    typedef Vector Point;
    Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
    Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
    Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}
    Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
    bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;}
    
    double Cross(Vector a,Vector b){
        return a.x*b.y-a.y*b.x;
    }
    double DisPP(Point a,Point b){
        Point t=a-b;
        return sqrt(t.x*t.x+t.y*t.y);
    }
    struct Line{
        Point s,t;
        Line(){}
        Line(Point p,Point v):s(p),t(v){}
    }a[N];
    bool isLSI(Line l1,Line l2){
        //puts("isLSI");
        //l1.s.print();l1.t.print();
        //l2.s.print();l2.t.print();
        Vector v=l1.t-l1.s,u=l2.s-l1.s,w=l2.t-l1.s;
        //printf("%d %d end
    ",sgn(Cross(v,u)),sgn(Cross(v,w)));
        return sgn(Cross(v,u))!=sgn(Cross(v,w))||!sgn(Cross(v,u));
    }
    int n,m;
    double x,y,x2,y2;
    bool check(Line l){
        if(sgn(DisPP(l.s,l.t))==0) return false;
        //printf("check
    ");
        //l.s.print();l.t.print();
        for(int i=1;i<=n;i++)
            if(!isLSI(l,a[i])) return false;
        return true;
    }
    
    void solve(){
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(check(Line(a[i].s,a[j].s))||check(Line(a[i].s,a[j].t))
                   ||check(Line(a[i].t,a[j].s))||check(Line(a[i].t,a[j].t)))
                    {puts("Yes!");return;}
        puts("No!");
    }
    
    int main(int argc, const char * argv[]) {
        int T=read();
        while(T--){
            n=read();
            for(int i=1;i<=n;i++){
                scanf("%lf%lf%lf%lf",&x,&y,&x2,&y2);
                a[i]=Line(Point(x,y),Point(x2,y2));
            }
            solve();
        }
        
        return 0;
    }
     
  • 相关阅读:
    css优化总结
    几种常用的图片格式
    css布局总结
    第四章复习题
    4.9,4.10
    4.8
    4.7指针
    libffi
    代理模式
    Redis 汇总
  • 原文地址:https://www.cnblogs.com/candy99/p/6353738.html
Copyright © 2011-2022 走看看