POJ 3304 Segments

Segments

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15584 Accepted: 4950

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 (x1, y1) and (x2, y2) 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!


题意:
给你几条线段,问存不存在这样一条直线,将所有线段投影在这条直线上后,所有的线段的投影至少有一个公共点。

思路:
计算几何入门题。
所有线段的投影至少有一个公共点,即存在一条直线与所有线段相交,枚举任意两点作直线,判断是否与全部直线相交。
线段 P1P2 与直线 Q1Q2 相交: (P1Q1)×(Q2Q1)(Q2Q1)×(P2Q1)0

几个坑点:
1. 当n<3时显然应该输出Yes!
2. 枚举点的时候两点离得太近(距离小于1e-8)时,可以看做一个点。

代码:

#include<iostream>
#include<set>
#include<cstdio>
#include<cmath>
#define maxn 105
using namespace std;

struct point
{
    double x,y;
}p[2*maxn];

struct segment
{
    double x1,x2,y1,y2;
}ss[maxn];

int n;

double cx(point a,point b)
{
    return (a.x)*(b.y)-(b.x)*(a.y);
}

bool pd(segment s,int a)
{
    double z1,z2;
    point c,d;
    c.x=ss[a].x1-s.x1;
    c.y=ss[a].y1-s.y1;
    d.x=s.x2-s.x1;
    d.y=s.y2-s.y1;
    z1=cx(c,d);
    c.x=ss[a].x2-s.x1;
    c.y=ss[a].y2-s.y1;
    z2=cx(d,c);
    if(z1*z2>=0) return 1;
    return 0;
}

bool judge(int a,int b)
{
    segment s;
    s.x1=p[a].x; s.y1=p[a].y;
    s.x2=p[b].x; s.y2=p[b].y;
    int i;
    for(i=0;i<n;i++)
    {
        if(!pd(s,i)) return 0;
    }
    return 1;
}

const double inv=0.00000001;
bool same(int a,int b)
{
    if(sqrt((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y))<inv) return 1;
    return 0;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int i,j;
        for(i=0;i<n;i++)
        {
            double a,b,c,d;
            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
            point A,B;
            A.x=a;A.y=b;
            B.x=c;B.y=d;
            p[2*i]=A;
            p[2*i+1]=B;
            segment s;
            s.x1=a;s.y1=b;s.x2=c;s.y2=d;
            ss[i]=s;
        }
        bool flag=0;
        if(n<=2) flag=1;
        for(i=0;i<2*n&&!flag;i++)
        {
            for(j=0;j<2*n;j++)
            {
                if(same(i,j)) continue;
                if(judge(i,j))
                {
                    flag=1;
                    break;
                }
            }
        }
        if(flag) printf("Yes!\n");
        else printf("No!\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值