poj 3304(利用叉积求是否存在直线与所给线段相交)

 

【题目大意】:给出n条线段,问你是否存在一条直线,使得每个线段与该直线至少有一个交点。

【解题思路】:没有思路,没有思路,还是没有思路。然后开始无止境的yy。 因为点的个数比较小,感觉n^3可以过,就开始yy枚举直线。一开始以为单纯的枚举之后就可以了,后来才发现要将左右线段投影到枚举出的直线上,取其相互覆盖的部分做垂线,这条线才是正解。

所以我们所要做的就是枚举假象中这条垂线,然后用叉积判断线段与之间的相交情况就可以了。

注意题目是坐标点的x,y的差分别小于1e-8,做的时候做成距离了,wa了一次

【代码】:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;

#define eps 1e-8
#define maxn 210;

struct Point{
    double x,y;
    Point() {}
    Point(double a,double b)
    {
        x=a,y=b;
    }
}point[maxn];

inline int sig(double k)
{
    return k<-eps?-1:k>eps;
}

inline double det(double x1, double y1, double x2, double y2)
{
    return x1*y2-x2*y1;
}

inline double xmult(Point o,Point a,Point b)
{
    return det(a.x-o.x,a.y-o.y,b.x-o.x,b.y-o.y);
}

int main()
{
    int cas,n;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d",&n);
        double tp1,tp2,tp3,tp4;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&tp1,&tp2,&tp3,&tp4);
            point[2*i-1]=Point(tp1,tp2);
            point[2*i]=Point(tp3,tp4);
        }
        bool flag=false;
        for(int i=1;i<=2*n&&flag==false;i++)
        {
            for(int j=1;j<=2*n;j++)
            {
                Point p,q;
                p=point[i];
                q=point[j];
                if(sig(p.x-q.x)==0 && sig(p.y-q.y)==0) continue;
                int l;
                for(l=1;l<=n;l++)
                    if(xmult(q,point[2*l-1],p)*xmult(q,point[2*l],p)>eps)
                    {
                        break;
                    }
                if(2*l-1>=2*n) {flag=true; break;}
            }
        }
        if (flag==true) printf("Yes!\n");
            else printf("No!\n");
    }
    return 0;
}


【运行结果】:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值