• 判断是否存在一条直线穿过所有的线段


    Segments

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 11   Accepted Submission(s) : 1
    Problem 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!
     
    题意:给出n个线段,然后判断是否存在一个直线,使所有的线段投影到该直线上之后形成的n个新的线段至少存在一个公共点,此意可以转化成该直线是否存在一条垂直线与所有的原线段都有交点;
    分析:该垂直线一定经过n个线段上的其中两个端点,所以枚举n个线段的端点即可
    程序:
    #include"string.h"
    #include"stdio.h"
    #include"iostream"
    #include"algorithm"
    #include"queue"
    #include"stack"
    #define M 5009
    #define N 100009
    #include"stdlib.h"
    #include"math.h"
    #define inf 10000000000000000LL
    #define INF 0x3f3f3f3f
    const double PI=acos(-1.0);
    #define eps 1e-10
    using namespace std;
    struct node
    {
        double x,y;
        node (){}
        node (double xx,double yy):x(xx),y(yy){}
        node operator -(node p)
        {
            return node (x-p.x,y-p.y);
        }
        double operator *(node p)
        {
            return x*p.y-y*p.x;
        }
        double operator ^(node p)
        {
            return x*p.x+y*p.y;
        }
    }p[12],q[12];
    int cnt;
    struct line
    {
        node s,e;
    }l[111];
    double max(double a,double b)
    {
        return a>b?a:b;
    }
    double min(double a,double b)
    {
        return a<b?a:b;
    }
    double cross(node a,node b,node c)
    {
        return (b-a)*(c-a);
    }
    double dot(node a,node b,node c)
    {
        return (b-a)^(c-a);
    }
    double len(node a)
    {
        return sqrt(a^a);
    }
    double dis(node a,node b)
    {
        return len(b-a);
    }
    int set_line_intersection(line l,line p)
    {
        if(dis(l.s,l.e)<eps)//当两个端点构成的直线长度是0的话return 0;因为该直线会与所有线段都有交点
            return 0;
        else if(cross(l.s,l.e,p.s)*cross(l.s,l.e,p.e)<eps)
            return 1;
        else
            return 0;
    }
    int slove(int n)
    {
        if(n<=2)
            return 1;
        int i,j,k,flag;
        for(i=1;i<=n;i++)
        {
            flag=1;
            for(k=1;k<=n;k++)
            {
                if(!set_line_intersection(l[i],l[k]))
                {
                    flag=0;
                    break;
                }
            }
            if(flag)
                return 1;
        }
        for(i=1;i<=n;i++)
        {
            for(j=i+1;j<=n;j++)
            {
                line l1;
                l1.s=l[i].s;
                l1.e=l[j].s;
                flag=1;
                for(k=1;k<=n;k++)
                {
                    if(!set_line_intersection(l1,l[k]))
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag)
                    return 1;
                flag=1;
                l1.s=l[i].s;
                l1.e=l[j].e;
                for(k=1;k<=n;k++)
                {
                    if(!set_line_intersection(l1,l[k]))
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag)
                    return 1;
                flag=1;
                l1.s=l[i].e;
                l1.e=l[j].s;
                for(k=1;k<=n;k++)
                {
                    if(!set_line_intersection(l1,l[k]))
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag)
                    return 1;
                flag=1;
                l1.s=l[i].e;
                l1.e=l[j].e;
                for(k=1;k<=n;k++)
                {
                    if(!set_line_intersection(l1,l[k]))
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag)
                    return 1;
            }
        }
        return 0;
    }
    int main()
    {
        int T,n,i;
        cin>>T;
        while(T--)
        {
            scanf("%d",&n);
            for(i=1;i<=n;i++)
                scanf("%lf%lf%lf%lf",&l[i].s.x,&l[i].s.y,&l[i].e.x,&l[i].e.y);
            if(slove(n))
                printf("Yes!
    ");
            else
                printf("No!
    ");
        }
        return 0;
    }
    


  • 相关阅读:
    github打开慢,甚至打不开
    在使用confluent-kafka-go 时遇到如下问题
    Istio Routing极简教程
    kubelet证书过期解决方法
    工具类docker for k8s
    selenium jar包 的下载地址,各版本都有
    使用TestNG进行多浏览器,跨浏览器和并行测试
    简单聊聊TestNG中的并发
    清除Git仓库多余文件及其历史记录 
    【MAVEN】maven系列--pom.xml标签详解
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348131.html
Copyright © 2020-2023  润新知