poj 3304 Segments 【判断是否存在一条直线与所有线段相交】

Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11471 Accepted: 3612

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 (x1y1) and (x2y2) 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| < 10-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!


题意:给定n条线段,问是否存在一条直线,使得所有线段在该直线的投影至少相交于一点。


思路:沿投影作直线的垂线,则垂线必定交于所有线段。枚举所有线段端点构造直线,判断该直线是否与所有线段相交。


问RYC,为什么枚举端点构造是个极限情况?RYC说了一大堆,蒟蒻没听懂。后来想找个反例,结果没找到。先这样吧 o(╯□╰)o


AC代码:


#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (100+10)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
struct Point
{
    double x, y;
    Point(){}
    Point(double X, double Y){
        x = X; y = Y;
    }
};
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);
}
double Cross(Point A, Point B){
    return A.x*B.y - A.y*B.x;
}
double Dot(Point A, Point B){
    return A.x*B.x + A.y*B.y;
}
int dcmp(double x)
{
    if(fabs(x) < eps)
        return 0;
    else
        return x < 0 ? -1 : 1;
}
bool operator == (const Point &a, const Point &b){
    return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
struct Line
{
    Point s, e;
    Line(){}
    Line(Point S, Point E){
        s = S; e = E;
    }
};
Line L[MAXN];
bool Check(Point a1, Point a2, Point b1, Point b2){//直线b1-b2 与线段a1-a2是否相交
    return Cross(b1-a1, b2-a1) * Cross(b1-a2, b2-a2) < eps;
}
bool judge(Point A, Point B, int n)
{
    if(A == B) return false;
    for(int i = 0; i < n; i++)
        if(!Check(L[i].s, L[i].e, A, B))
            return false;
    return true;
}
int main()
{
    int t; Ri(t);
    W(t)
    {
        int n; Ri(n);
        for(int i = 0; i < n; i++)
        {
            Rf(L[i].s.x); Rf(L[i].s.y);
            Rf(L[i].e.x); Rf(L[i].e.y);
        }
        bool flag = false;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(judge(L[i].s, L[j].s, n))
                {
                    flag = true;
                    break;
                }
                if(judge(L[i].s, L[j].e, n))
                {
                    flag = true;
                    break;
                }
                if(judge(L[i].e, L[j].s, n))
                {
                    flag = true;
                    break;
                }
                if(judge(L[i].e, L[j].e, n))
                {
                    flag = true;
                    break;
                }
            }
            if(flag)
                break;
        }
        if(flag)
            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、付费专栏及课程。

余额充值