POJ 3304 Segments

Segments
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7363   Accepted: 2207

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!

Source

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

const int eps=1e-8;

struct point{
    double x,y;
}p[410];

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

double abs(double a){
    if(a<0)
        return -a;
    return a;
}

bool work(int n){
    int flag;
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++){
            if(abs(p[j].x-p[i].x)>eps || abs(p[j].y-p[i].y)>eps){
                flag=1;
                for(int k=1;k<=n;k+=2)
                    if(xmult(p[i],p[j],p[k])*xmult(p[i],p[j],p[k+1])>0){
                        flag=0;
                        break;
                    }
                if(flag)
                    return 1;
            }
        }
    return 0;
}

int main(){

    //freopen("input.txt","r",stdin);

    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        int cnt=1;
        for(int i=1;i<=n;i++,cnt+=2)
            scanf("%lf%lf%lf%lf",&p[cnt].x,&p[cnt].y,&p[cnt+1].x,&p[cnt+1].y);
        if(work(cnt-1))
            printf("Yes!\n");
        else
            printf("No!\n");
    }
    return 0;
}

 

posted @ 2013-03-14 21:58  Jack Ge  阅读(253)  评论(0编辑  收藏  举报