POJ 3304 Segments

注意判断大于 小于 等于0的时候 

要用eps 而不是用0

见cross相乘判断 用0即WA

/*
    尝试 向量,点都用Point表示
*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define db double
using namespace std;
const db eps=1e-8;
const int N=150;
struct Point
{
    db x,y;
    Point(){}
        Point(db _x,db _y) {x = _x;y = _y;} 
};
struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e) {s=_s;e=_e;} 
};
Line L[N];
Point operator + (Point A,Point B) {return Point(A.x+B.x,A.y+B.y);} 
Point operator - (Point A,Point B) {return Point(A.x-B.x,A.y-B.y);} 
db operator * (Point A,Point B) {return A.x*B.x+A.y*B.y;} 
db operator ^ (Point A,Point B) {return A.x*B.y-A.y*B.x;} 
db dist(Point s,Point e) {return sqrt((s-e)*(s-e));} 
db cross(Point a,Point b,Point c) {return (a-c)^(b-c);} 
int n;
bool check(Point s,Point e)
{
    if(dist(s,e)<=eps) return 0;
    for(int i=1;i<=n;i++)
        if(cross(L[i].s,s,e)*cross(L[i].e,s,e)>eps) 
            return 0;
    return 1;
}
/*
题目转化为求是否存在一条直线与所有线段相交
巧妙的枚举两个线段的端点来确定一条直线 然后判断该直线是否与全部线段有交点 
*/
int main()
{
    int T; scanf("%d",&T); while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            db x1,x2,y1,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            L[i]=Line(Point(x1,y1),Point(x2,y2));
        }
        bool flag=0;
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        if(check(L[i].s,L[j].s)||check(L[i].s,L[j].e)
            ||check(L[i].e,L[j].s)||check(L[i].e,L[j].e))
        {
            flag=1;break;
        }
        if(flag) puts("Yes!");
        else puts("No!");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/lxy8584099/p/10417835.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值