Poj 3304 Segments

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 x1y1x2y2 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!


第一次看的时候   居然没看懂此题...QAQ

题意: 是否存在一条直线与所有线段都有交点 

做法:每两条线段的各一个端点,比如线段AB 和线段CD 选取AC AD BC BD依次连成直线,再依次判断剩下的线段是否都和这条直线有交点。 用到了叉积来判断P3与P1P1的关系   因为x1y2+x3y1+x2y3-x3y2-x2y1-x1y3可能爆了 错了17次. 囍!


#include<iostream>
#include<cmath>
#include <stdio.h>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
#define MAX 150
#define MIN 1e-8
int n;
struct Point
{
    double x,y;
} Left[MAX],Right[MAX]; //端点存入数组
double det(Point p1,Point p2,Point p3)
{
    return (p1.x-p3.x)*(p2.y-p3.y)-(p2.x-p3.x)*(p1.y-p3.y);//判断点p3是否在线段p1p2的左右
    //  x1y2+x3y1+x2y3-x3y2-x2y1-x1y3  不要这样直接乘..可能会爆掉
}
int judge(Point p1,Point p2)
{
    if(abs(p1.x-p2.x) < MIN && abs(p1.y-p2.y) < MIN)
        return 0;
    for(int i=0; i<n; i++)
        if(det(p1, p2, Left[i])*det(p1, p2, Right[i]) > MIN) return 0;//每个点都进行判断
    return 1;
}
int main()
{
    int T,result;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf%lf%lf",&Left[i].x,&Left[i].y,&Right[i].x,&Right[i].y);
        }
        result=0;
        if(n<3) result=true;//小于三条线段 肯定能够找到一条直线满足条件
        for(int i=0; i<n; i++)
        {
            for(int j=i+1; j<n; j++) //从i+1开始 因为之前的情况已经枚举过
            {
                if(result) break;
                if(judge(Left[i], Left[j])) result = 1;
                else if(judge(Left[i], Right[j])) result = 1;
                else if(judge(Right[i], Left[j])) result = 1;
                else if(judge(Right[i], Right[j])) result = 1;
            }
            if(result) break;
        }
        if(result) printf("Yes!\n");
        else printf("No!\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值