poj 3304 找一条直线穿过所有线段

题目链接:http://poj.org/problem?id=3304

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 105;
const int maxe = 20000;
const int INF = 0x3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1.0);

struct Point{
    double x,y;
    Point(double x=0, double y=0) : x(x),y(y){ }    //构造函数
};
typedef Point Vector;

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);
}
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;
}

double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y; }
double Cross(Vector A, Vector B)  { return A.x*B.y - A.y * B.x; }
double Length(Vector A)    { return sqrt(Dot(A,A)); }

bool SegmentLineIntersection(Point a1,Point a2,Point b1,Point b2){  //a1a2是直线,b1b2是线段;
    double c1 = Cross(a2-a1,b1-a1), c2 = Cross(a2-a1,b2-a1);

    return dcmp(c1) * dcmp(c2) < 0 || dcmp(c1) == 0 || dcmp(c2) == 0;
}

Point read_point(){
    Point A;
    scanf("%lf %lf",&A.x,&A.y);
    return A;
}

/******************************分割线*******************************/

Point P[maxn][2];
int n;

int main()
{
   //freopen("E:\acm\input.txt","r",stdin);
    int T;
    cin>>T;
    while(T--){
        cin>>n;
        for(int i=1;i<=n;i++){
            P[i][0] = read_point();
            P[i][1] = read_point();
        }
        bool ans = false;
        for(int i=1;i<n;i++)
          for(int k=0;k<=1;k++)
            for(int j=i+1;j<=n;j++)
               for(int m=0;m<=1;m++){  //确定两个端点P[i][k]和P[j][m];接下来判断过这两点直线是否穿过所有线段;
                  if(P[i][k] == P[j][m])  continue;
                  int s;
                  for(s=1;s<=n;s++){
                      if(!SegmentLineIntersection(P[i][k],P[j][m],P[s][0],P[s][1])) break;
                  }
                  if(s == n+1) { ans = true;  goto print; }
            }

        print:
        if(ans || n==1)  printf("Yes!
");  //n == 1没考虑,WA了两次看discuss别人说的;
        else     printf("No!
");
    }
}
View Code
原文地址:https://www.cnblogs.com/acmdeweilai/p/3251930.html