您的位置:首页 > 其它

Poj-3304-Segments

2015-11-09 19:49 169 查看
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!

这道题的题意是给你一个数T,表示有T个数据,每个数据都是先输入一个数n,代表线段有几条,然后接下来n行都有四个数(浮点型小数)x1`,y1,x2,y2,(x1,y1)和(x2,y2)分别表示线段的两个端点,然后问你是否存在一条直线,使所有的线段投影在其上,如果有交点,输出“Yes!",否则输出“No!".

思路:若存在这样一条直线,过投影相交区域作直线的垂线,垂足为投影交点,该垂线必定与每条线段相交,问题就转化为了是否存在一条线和所有线段相交;若存在一条直线与所有线段相机相交,将该线旋转,平移,直到不能再动为止,此时该直线必定经过这些线段的某两个端点;因此我们可以枚举两个线段的端点,连成一条直线,看是否与其它的线段都相交,如果相交,则存在这样的直线。

注意:枚举时注意如果两点距离小于10的-8次方,则看为同一个点。若要判断直线与线段是否相交,则只需用叉积判断线段的两个端点不同时在直线的一边即可

叉乘的性质: 若向量P,Q

P*Q<0;则Q在P的右边

P*Q=0;则Q与P平行或重合

P*Q>0;则Q在P的左边

代码如下:

#include <algorithm>

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <math.h>

using namespace std;

//定义mi为10的-8次方

#define mi 1e-8

struct note{

    double x1;

    double y1;

    double x2;

    double y2;

};

int T,n;

struct note a[105];

//判断两点距离是否小于10的-8次方

int juli(double x1,double y1,double x2,double y2)

{

    if(sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))<=mi)

        return 1;

    else

        return 0;

}

//叉积判断一个线段端点在所枚举直线的左边,右边,或者直线上

int chacheng(double x1,double y1,double x2,double y2,double x3,double y3)

{

    if((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1)>0)

        return 1;

    else if((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1)==0)

        return 0;

    else

        return -1;

}

//判断是否相交

int xiangjiao(double x1,double y1,double x2,double y2)

{

    //判断两个端点是否重合

    if(juli(x1,y1,x2,y2))

        return 0;

    //判断该直线是否与所有线段都有交点

    for(int i=1;i<=n;i++)

    {

        //判断一条线段的两个端点是否在该直线的同一边

        int cha1,cha2;

        cha1=chacheng(x1,y1,x2,y2,a[i].x1,a[i].y1);

        cha2=chacheng(x1,y1,x2,y2,a[i].x2,a[i].y2);

        if(cha1*cha2>0)

            return 0;

    }

    //如果这两点不重合,且所有的线段的端点不同时在这两点组成的直线上,即说明存在这样的直线

    return 1;

}

int main()

{

    scanf("%d",&T);

    while(T--)

    {

        scanf("%d",&n);

        for(int i=1;i<=n;i++)

            scanf("%lf %lf %lf %lf",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);

        //对n等于1的情况进行单独判断

        if(n==1)

        {

            printf("Yes!\n");

            continue;

        }

        //book作为直线存在的标记

        int book=0;

        //枚举直线

        for(int i=1;i<=n;i++)

        {

            for(int j=i+1;j<=n;j++)

                //对两个点的四个端点组成的四个直线同时进行判断

                if(xiangjiao(a[i].x1,a[i].y1,a[j].x1,a[j].y1)||xiangjiao(a[i].x2,a[i].y2,a[j].x1,a[j].y1)||xiangjiao(a[i].x1,a[i].y1,a[j].x2,a[j].y2)||xiangjiao(a[i].x2,a[i].y2,a[j].x2,a[j].y2))

                {

                    //如果存在这样的直线,标记,退出循环

                    book=1;

                    break;

                }

            //如果存在这样的直线,退出循环

            if(book==1)

                break;

        }

        //输出

        if(book==1)

            printf("Yes!\n");

        else

            printf("No!\n");

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: