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

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!\n");
        else
            printf("No!\n");
    }
    return 0;
}


转载于:https://www.cnblogs.com/mypsq/p/4348131.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值