POJ3304 Segments

原题传送门:http://poj.org/problem?id=3304
博主的中文题面(数据水):
https://www.luogu.org/problemnew/show/T22834  

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 x1 y1 x2 y2 follow, in which (x1, y1) and (x2, y2) 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| < 1e-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!

题目大意

问是否有一条能与平面内所有线段相交的直线

题解

英文题面的直接意思并不是中文题面那样直接明了,具体的证明大家自己用几何画板做做垂线感性证明一下吧。。。因为一定能找到一条满足题意的直线使其过线段中的某两个端点(感性理解)。所以,我们只需要O(n^2)枚举所有端点,O(n)挨个检验这条直线与所有线段的交点即可水过。

代码
#include<cstdio>
#include<cstring>
#define db double
using namespace std;
const int M=105;
const db eps=1e-8;
struct pt{db x,y;};
struct li{pt f,t;};
db operator * (pt a,pt b){return a.x*b.y-a.y*b.x;}
pt operator - (pt a,pt b){return (pt){a.x-b.x,a.y-b.y};}
int sig(db a){return (a>eps)-(a<-eps);}
int n,top;
pt pp[M<<1];
li ll[M];
void in()
{
    db a,b,c,d;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    {
        scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
        pp[++top]=(pt){a,b};
        pp[++top]=(pt){c,d};
        ll[i]=(li){pp[top],pp[top-1]};
    }
}
db area(pt a,pt b,pt c){return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);}
int test(pt a,pt b,pt c,pt d)
{
    int d1=sig(area(a,b,c));
    int d2=sig(area(a,b,d));
    if((d1^d2)==-2) return 1;
    if(d1==0||d2==0) return 2;
    return 0;
}
bool check(pt a,pt b)
{
    if(!sig(a.x-b.x)&&!sig(a.y-b.y)) return 0;
    for(int i=1;i<=n;++i)
        if(test(a,b,ll[i].f,ll[i].t)==0) return 0;
    return 1;
}
void ac()
{
    bool flag=0;
    for(int i=1;i<top;++i)
    for(int j=i+1;j<=top;++j)
    if(check(pp[i],pp[j])) {flag=1;break;}
    if(flag) printf("Yes!\n");
    else printf("No!\n");
//  for(int i=1;i<=top;++i)
//  printf("%lf %lf\n",pp[i].x,pp[i].y);
    memset(pp,0,sizeof(pp));
    memset(ll,0,sizeof(ll));
    top=0;
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int i=1;i<=T;++i)
    in(),ac();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值