POJ 3304 Segments

Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9129 Accepted: 2801

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!

刚开始的时候想错了,以为只要枚举每条边就可以了,若给出的线段中只要有交点就可以了,(好愚蠢的想法!)
 题意:
     要求所有线段的投影交点至少有一个公共点,也就是说,存在一条直线穿过每条线段。
 存在的BUG:
          考虑n==1的时候,输出 是的。
         考虑题目中给出的精度问题,如果枚举各点的过程中,两点之间的距离小于一个精度值,则忽略掉这条直线。
方法:
     枚举给出的各个点,两点确定一条直线
     枚举给出的线段
              如果这条直线可以穿过枚举的所有线段即停止枚举直线,输出  是的,如果枚举出了一个线段不与这条直线有交点,即停止枚举线段,输出 不是。
         (注意在这里说的直线与线段的区别)。


#include <stdio.h>
#include <math.h>

struct point
{
    double x;
    double y;
}p[250];

struct line
{
    struct point a;
    struct point b;
}l[105];

int sig(double k)
{
    if(fabs(k)<1e-8)
        return 0;
    else
        return (k>0)?1:-1;
}

int mul(struct point p1,struct point p2,struct point p3)
{
    return sig((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y));
}

int main (void)
{
    //freopen("3304.txt","r",stdin);
    int t,n,i,j,k,h,ok,m;
    while(scanf("%d",&t)==1)
    {
        for(i=1;i<=t;i++)
        {
            scanf("%d",&n);
            k=0;
            ok=1;
            for(j=1;j<=n;j++)
            {
                scanf("%lf%lf%lf%lf",&l[j].a.x,&l[j].a.y,&l[j].b.x,&l[j].b.y);
                p[++k].x=l[j].a.x; p[k].y=l[j].a.y;
                p[++k].x=l[j].b.x; p[k].y=l[j].b.y;
                //if(sqrt((l[j].b.x-l[j].a.x)*(l[j].b.x-l[j].a.x)+(l[j].b.y-l[j].a.y)*(l[j].b.y-l[j].a.y))<1e-8)
                   // ok=0;
            }
           // if(ok==0)
                //continue;
            if(n==1) printf("Yes!\n");
            else
            {

            for(j=1;j<=k;j++)       //枚举各点,判断是否有线段不通过这两点构成的线段
            {
                for(h=j+1;h<=k;h++)
                {
                    if(sqrt((p[h].x-p[j].x)*(p[h].x-p[j].x)+(p[h].y-p[j].y)*(p[h].y-p[j].y))<1e-8)
                        continue;
                    ok=1;
                    for(m=1;m<=n;m++)
                    {

                        if(mul(p[j],p[h],l[m].a)*mul(p[j],p[h],l[m].b)>0)
                        {
                            ok=0;
                            break;
                        }
                    }
                    if(ok)
                        break;
                }
                if(ok)
                    break;
            }
            if(ok) 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、付费专栏及课程。

余额充值