Fork me on GitHub

POJ 3304 Segments --枚举,几何

题意: 给n条线段,问有没有一条直线,是每条线段到这条直线上的投影有一个公共点。

解法: 有公共点说明有一条这条直线的垂线过所有线段,要找一条直线过所有线段,等价于从所有线段中任选两端点形成的直线存在可以穿过所有的线段的直线(可将A平移至一条线段端点,然后绕这点旋转,使A过另一条线段端点),然后O(n^2)的枚举找任意两个线段的两个端点,还要找自己这条线段的两个端点,形成一条直线

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define eps 1e-8
using namespace std;
#define N 100017

struct Point{
    double x,y;
    Point(double x=0, double y=0):x(x),y(y) {}
    void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
struct Circle{
    Point c;
    double r;
    Circle(){}
    Circle(Point c,double r):c(c),r(r) {}
    Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); }
    void input() { scanf("%lf%lf%lf",&c.x,&c.y,&r); }
};
struct Line{
    Point p;
    Vector v;
    double ang;
    Line(){}
    Line(Point p, Vector v):p(p),v(v) { ang = atan2(v.y,v.x); }
    Point point(double t) { return Point(p.x + t*v.x, p.y + t*v.y); }
    bool operator < (const Line &L)const { return ang < L.ang; }
};
int dcmp(double x) {
    if(x < -eps) return -1;
    if(x > eps) return 1;
    return 0;
}
template <class T> T sqr(T x) { return x * x;}
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; }
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
Vector VectorUnit(Vector x){ return x / Length(x);}
Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);}
double angle(Vector v) { return atan2(v.y, v.x); }

bool OnSegment(Point P, Point A, Point B) {
    return dcmp(Cross(A-P,B-P)) == 0 && dcmp(Dot(A-P,B-P)) < 0;
}
double DistanceToSeg(Point P, Point A, Point B)
{
    if(A == B) return Length(P-A);
    Vector v1 = B-A, v2 = P-A, v3 = P-B;
    if(dcmp(Dot(v1, v2)) < 0) return Length(v2);
    if(dcmp(Dot(v1, v3)) > 0) return Length(v3);
    return fabs(Cross(v1, v2)) / Length(v1);
}
double DistanceToLine(Point P, Point A, Point B){
    Vector v1 = B-A, v2 = P-A;
    return fabs(Cross(v1,v2)) / Length(v1);
}
Point GetLineIntersection(Line A, Line B){
    Vector u = A.p - B.p;
    double t = Cross(B.v, u) / Cross(A.v, B.v);
    return A.p + A.v*t;
}

//data segment
struct Seg{
    Point P[2];
}seg[106];
//data ends

int main()
{
    int t,n,i,j,k,h,s;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            seg[i].P[0].input(), seg[i].P[1].input();
        if(n == 1)
        {
            puts("Yes!");
            continue;
        }
        bool flag = 0;
        Point A,B;
        for(i=1;i<=n;i++)
        {
            for(j=i+1;j<=n;j++)
            {
                for(k=0;k<2;k++)       //Seg[i]'s Point
                {
                    for(h=0;h<2;h++)   //Seg[j]'s Point
                    {
                        A = seg[i].P[k], B = seg[j].P[h];
                        if(A == B) continue;
                        for(s=1;s<=n;s++)
                        {
                            if(s == i || s == j) continue;
                            if(dcmp(Cross(seg[s].P[0]-A,B-A)*Cross(seg[s].P[1]-A,B-A)) > 0)
                                break;
                        }
                        if(s == n+1)
                        {
                            flag = 1;
                            break;
                        }
                    }
                }
                int cnt = 0;
                for(k=i;cnt<=1;k=j,cnt++)
                {
                    A = seg[k].P[0], B = seg[k].P[1];
                    if(A == B) continue;
                    for(s=1;s<=n;s++)
                    {
                        if(s == k) continue;
                        if(dcmp(Cross(seg[s].P[0]-A,B-A)*Cross(seg[s].P[1]-A,B-A)) > 0)
                            break;
                    }
                    if(s == n+1)
                    {
                        flag = 1;
                        break;
                    }
                }
            }
            if(flag) break;
        }
        if(flag) puts("Yes!");
        else     puts("No!");
    }
    return 0;
}
View Code

 

posted @ 2014-11-19 00:51  whatbeg  阅读(230)  评论(0编辑  收藏  举报