zoukankan      html  css  js  c++  java
  • poj3304 Segments

    题目描述:

    vjudge

    POJ

    题解:

    计算几何,坑点极多。

    首先很明显存在一条直线穿过所有线段即$Yes$。

    考虑枚举任意两个端点。

    注意同一条线段上的也要枚举!

    注意$eps=1e-8$!

    注意两点重合就不用判了!

    代码:

    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N = 150;
    const double eps = 1e-8;
    struct Point
    {
        double x,y;
        Point(){}
        Point(double x,double y):x(x),y(y){}
        Point operator - (const Point&a)const{return Point(x-a.x,y-a.y);}
        double operator ^ (const Point&a)const{return x*a.y-y*a.x;}
    };
    typedef Point Vector;
    struct Line
    {
        Point p;
        Vector v;
        Line(){}
        Line(Point p,Vector v):p(p),v(v){}
    };
    int n;
    Point s[N][2];
    int dcmp(double x)
    {
        if(fabs(x)<eps)return 0;
        return x>0?1:-1;
    }
    bool diff(Line l,Point a,Point b)
    {
        return dcmp(l.v^(a-l.p))*dcmp(l.v^(b-l.p))<=0;
    }
    int check(Point a,Point b)
    {
        if(!dcmp(a.x-b.x)&&!dcmp(a.y-b.y))return 0;
        Line l = Line(a,b-a);
        for(int i=1;i<=n;i++)
            if(!diff(l,s[i][0],s[i][1]))return 0;
        return 1;
    }
    void work()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%lf%lf%lf%lf",&s[i][0].x,&s[i][0].y,&s[i][1].x,&s[i][1].y);
        int FG = 0;
        for(int i=1;!FG&&i<=n;i++)for(int j=i;!FG&&j<=n;j++)
            if(check(s[i][0],s[j][0])||check(s[i][0],s[j][1])||check(s[i][1],s[j][0])||check(s[i][1],s[j][1]))FG=1;
        puts(FG?"Yes!":"No!");
    }
    int T;
    int main()
    {
        scanf("%d",&T);
        while(T--)work();
        return 0;
    }
    View Code
  • 相关阅读:
    Virtualbox中不能为虚拟机打开一个新任务的原因及解决方法
    CSS 制作三角形原理剖析
    java 执行jar指定log4j.properties文件位置
    Git submodules
    IO模型
    Error during WebSocket handshake 403
    Proto(协议缓冲区)
    gRPC
    Node.js 模块机制
    Node.js 模块机制及常见面试问题解答
  • 原文地址:https://www.cnblogs.com/LiGuanlin1124/p/10981368.html
Copyright © 2011-2022 走看看