Segments - POJ 3304 线段相交

本文探讨了一个几何问题,即在二维空间中,通过给定的线段集合,判断是否存在一条直线使得所有线段在该直线上投影后至少有一个公共点。提出了一种算法来解决此问题,并提供了详细的输入输出样例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10570 Accepted: 3281

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条线段,判断是否存在有一条直线,满足所有的线段在直线上投影后至少有一个公共点

思路:原命题等价为存在一条直线穿过所有的线段(易知过公共点且垂直于所求直线的直线符合条件,设为直线a),该命题又等价于从所有线段中任选两端点形成的直线存在可以穿过所有的线段的直线(可将a平移至一条线段端点,然后绕这点旋转,使a过另一条线段端点)

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
}p[210];
typedef Point Vector;
double eps=1e-8;
Vector operator + (Vector A,Vector B){return Point(A.x+B.x,A.y+B.y);}
Vector operator - (Vector A,Vector B){return Point(A.x-B.x,A.y-B.y);}
int dcmp(double x){return (x>eps)-(x<-eps);}
double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}
int SegmentInterSection(Point a1,Point a2,Point b1,Point b2)
{
    int d1,d2;
    d1=dcmp(Cross(a2-a1,b1-a1));
    d2=dcmp(Cross(a2-a1,b2-a1));
    if((d1^d2)==-2)
      return 1;
    if(d1==0 || d2==0)
      return 2;
    return 0;
}
int T,t,n,m;
bool flag;
bool test(Point a1,Point a2)
{
    int i,j,k;
    if(dcmp(a1.x-a2.x)==0 && dcmp(a1.y-a2.y)==0)
      return 0;
    for(i=1;i<=2*n;i+=2)
       if(SegmentInterSection(a1,a2,p[i],p[i+1])==0)
         return 0;
    return 1;
}
void solve()
{
    int i,j;
    for(i=1;i<=n*2;i++)
       for(j=i+1;j<=n*2;j++)
          if(test(p[i],p[j]))
          {
              flag=1;
              return;
          }
}
int main()
{
    int i,j,k;
    scanf("%d",&T);
    for(t=1;t<=T;t++)
    {
        scanf("%d",&n);
        for(i=1;i<=n*2;i++)
           scanf("%lf%lf",&p[i].x,&p[i].y);
        flag=0;
        solve();
        if(flag)
          printf("Yes!\n");
        else
          printf("No!\n");

    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值