zoukankan      html  css  js  c++  java
  • 判断是否存在一条直线穿过所有的线段

    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;
    }
    


  • 相关阅读:
    webpack打包的项目,如何向项目中注入一个全局变量
    移动端微信H5兼容ios的自动播放音视频
    移动端H5解决键盘弹出时之后滚动位置发生变化的问题
    微信网页开发,如何在H5页面中设置分享的标题,内容以及缩略图
    React实现组件缓存的一种思路
    React编写一个移动H5的纵向翻屏组件
    如何手写一个react项目生成工具,并发布到npm官网
    Puppeteer爬取单页面网站的数据示例
    modelsim中objects窗口为空的解决办法
    Lattice Diamond与modelsim联合仿真环境设置
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348131.html
Copyright © 2011-2022 走看看