poj 3304 Segments(计算几何)

Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12070 Accepted: 3819

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!

Source

[Submit]   [Go Back]   [Status]   [Discuss]

题目大意:给出平面内的n条线段,求是否存在一条直线使得所有线段的投影在直线上有至少一个共同的交点。

题解:这道题直接求解可能不太好搞,但是我们可以逆着想,如果存在一条直线使得所有线段的投影在直线上有共同的交点,那么一定存在一条与该投影直线垂直的直线与所有线段都有交点。因为只有100条线段,所我们可以暴力枚举线段中的两个端点,因为两点确定一条直线,利用确定的直线与所有线段判断是否相交,就能得到答案。为什么可以用端点确定的直线来判定呢?因为如果存在一条与所有线段都相交的直线,那么这条直线通过平移或者旋转的方式一定可以通过至少某两个端点。(实在想不过来就画画图)

如果线段P1P2和直线Q1Q2相交,则P1P2跨立Q1Q2,即:( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) >= 0。

用这个公式判定即可,注意叉积不满足交换率,所以公式必须严格满足。

还需注意如果枚举到的端点重合,则无法确定一条直线,此时不能用这时的结果去判断。

#include<iostream>    
#include<cstdio>    
#include<cstring>    
#include<cmath>    
#include<algorithm>    
#define N 130    
#define LL long long    
using namespace std;    
int t,m,n,cnt;    
struct point    
{    
    double x,y;    
    point(){x=y=0;};    
    point operator -(const point &a)    
    {    
        point t;     
        t.x=x-a.x; t.y=y-a.y;    
        return t;    
    }    
    bool operator ==(const point &a)const
    {
    	return x==a.x&&y==a.y;
    }
};point p[4*N];    
struct segment    
{    
    double  p1,p2,q1,q2;    
    segment(){p1=p2=q1=q2=0;};    
};segment a[N];    
struct vector    
{    
    double x,y;    
    vector(){x=y=0;};    
    vector operator =(const point &a)    
    {    
        this->x=a.x; this->y=a.y;    
        return *this;    
    }    
    double operator *(const vector &a)    
    {    
        return (double)x*a.y-(double)y*a.x;    
    }    
};    
bool solve(int x,int  y)    
{    
    vector t,t1;    
    t=p[y]-p[x];    
    if (p[y]==p[x]) return false;
    for (int i=1;i<=cnt;i+=2)    
     {    
        vector k; k=p[i]-p[x];    
        vector l; l=p[i+1]-p[x];   
        if ((k*t)*(t*l)<0) return false;    
     }    
    return true;    
}    
int main()    
{      
    scanf("%d",&t);    
    for (int T=1;T<=t;T++)    
     {    
        scanf("%d",&n);    
        cnt=0;    
        for (int i=1;i<=n;i++)    
        {    
         scanf("%lf%lf%lf%lf",&a[i].p1,&a[i].p2,&a[i].q1,&a[i].q2);    
         cnt++; p[cnt].x=a[i].p1; p[cnt].y=a[i].p2;    
         cnt++; p[cnt].x=a[i].q1; p[cnt].y=a[i].q2;    
        }    
        bool flag=false;    
        for (int i=1;i<=cnt-1;i++)    
        {    
         bool f=false;    
         for (int j=i+1;j<=cnt;j++)    
           if (solve(i,j)) {    
            f=true;    
            break;    
           }    
         if (f) {    
            flag=true;    
            break;    
         }    
         }    
         if (flag) printf("Yes!\n");    
         else printf("No!\n");    
     }    
}    



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值