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

来源:互联网 发布:气体检测仪数据 编辑:程序博客网 时间:2024/05/21 19:47

Segments
Time Limit: 1000MS Memory Limit: 65536KTotal 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

321.0 2.0 3.0 4.04.0 5.0 6.0 7.030.0 0.0 0.0 1.00.0 1.0 0.0 2.01.0 1.0 2.0 1.030.0 0.0 0.0 1.00.0 2.0 0.0 3.01.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|1using 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
原创粉丝点击