POJ 3304 判断线段 与 直线 相交

题意:给出n条线段,判断是否存在有一条直线,满足所有的线段在直线上投影后至少有一个公共点(与所有线段都会相交)

开始想错了,因为是再已经给的线段中 是否存在选一条做直线,使得它与所有线段相交;

这样也能过sample input;

害我找了一天了;

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstdio>
using namespace std;
const double eps = 1e-8;
const int maxn = 100+5;

struct Point
{
    double x;
    double y;
    Point(double a = 0, double b = 0){ x = a; y = b; }
} P[maxn*2];

struct Line
{
    Point u;
    Point v;
} L;
int T,N;

int Sig(double x)  
{
    return (x > eps) - (x < -eps);
//    return x < -eps? -1 : x > eps;  //这个也行
}

double Mult(Point p1, Point p2)  // 两点 叉积
{
    return p1.x*p2.y - p1.y*p2.x;
}

double Mult(Point p0, Point p1, Point p2)  //三点 叉积
{
    return (p1.x-p0.x)*(p2.y-p0.y) - (p2.x-p0.x)*(p1.y-p0.y);
}

int same_site(Point p1, Point p2, Line L)  //point p1 & p2 on the same site of line L
{
    int a = Sig(Mult(L.u,L.v,p1));
    int b = Sig(Mult(L.u,L.v,p2));
    return a*b > 0;
}

int Judge(Line L)
{
    if(Sig(L.u.x - L.v.x)==0 && Sig(L.u.y - L.v.y)==0)
        return false;
    for(int i = 1; i < 2*N; i += 2)
        if(same_site(P[i],P[i+1],L))
            return false;
    return true;
}

int slove()
{
    for(int i = 1; i < N*2; i++)
    {
        for(int j = i+1; j <= N*2; j++)
        {
            L.u.x = P[i].x; L.u.y = P[i].y;
            L.v.x = P[j].x; L.v.y = P[j].y;
            if(Judge(L))//如果成功,即找到了这条直线
                return true;
        }
    }
    return false;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in","r",stdin);
#endif
    cin>>T;
    while(T--)
    {
        cin>>N;
        for(int i = 1; i <= N*2; i++)
        {
            cin>>P[i].x>>P[i].y;
        }
        if(slove())
            puts("Yes!");
        else
            puts("No!");
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值