poj3304 Segments

题目描述:

vjudge

POJ

题解:

计算几何,坑点极多。

首先很明显存在一条直线穿过所有线段即$Yes$。

考虑枚举任意两个端点。

注意同一条线段上的也要枚举!

注意$eps=1e-8$!

注意两点重合就不用判了!

代码:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 150;
const double eps = 1e-8;
struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
    Point operator - (const Point&a)const{return Point(x-a.x,y-a.y);}
    double operator ^ (const Point&a)const{return x*a.y-y*a.x;}
};
typedef Point Vector;
struct Line
{
    Point p;
    Vector v;
    Line(){}
    Line(Point p,Vector v):p(p),v(v){}
};
int n;
Point s[N][2];
int dcmp(double x)
{
    if(fabs(x)<eps)return 0;
    return x>0?1:-1;
}
bool diff(Line l,Point a,Point b)
{
    return dcmp(l.v^(a-l.p))*dcmp(l.v^(b-l.p))<=0;
}
int check(Point a,Point b)
{
    if(!dcmp(a.x-b.x)&&!dcmp(a.y-b.y))return 0;
    Line l = Line(a,b-a);
    for(int i=1;i<=n;i++)
        if(!diff(l,s[i][0],s[i][1]))return 0;
    return 1;
}
void work()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%lf%lf%lf%lf",&s[i][0].x,&s[i][0].y,&s[i][1].x,&s[i][1].y);
    int FG = 0;
    for(int i=1;!FG&&i<=n;i++)for(int j=i;!FG&&j<=n;j++)
        if(check(s[i][0],s[j][0])||check(s[i][0],s[j][1])||check(s[i][1],s[j][0])||check(s[i][1],s[j][1]))FG=1;
    puts(FG?"Yes!":"No!");
}
int T;
int main()
{
    scanf("%d",&T);
    while(T--)work();
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/LiGuanlin1124/p/10981368.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值