poj3304(是否存在一条直线与所有给出线段相交

  题意:给出n条线段,问你是否存在一条直线让他与所有线段相交。

  思路:枚举两条直线的起点和终点做一条直线,看他是否与所有线段相交。

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include <time.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 400005+1000;
const int sigma=26;
const ll mod = 1000000007;
const int INF = 0x3f3f3f;
const db eps = 1e-8;
struct point {
    db x, y;
}s[maxn], e[maxn];
int n;

db mul(point s, point e, point op) {
    return (s.x-op.x)*(e.y-op.y)-(e.x-op.x)*(s.y-op.y);
}
bool seg(point p1, point p2) {
    if (abs(p1.x-p2.x)<eps && abs(p1.y-p2.y)<eps)  return 0;
    for (int i=0; i<n; i++) {
        if (mul(p1, p2, s[i])*mul(p1, p2, e[i])>eps)  return 0;
    }
    return 1;
}
void solve() {
    scanf("%d", &n);
    for (int i=0; i<n; i++) {
        scanf("%lf%lf", &s[i].x, &s[i].y);
        scanf("%lf%lf", &e[i].x, &e[i].y);
    }
    if (n<3)  {
        puts("Yes!");
        return ;
    }
    for (int i=0; i<n; i++) {
        for (int j=i+1; j<n; j++) {
            if (seg(s[i], s[j])||seg(s[i],e[j])
                ||seg(e[i], s[j])||seg(e[i], e[j])) {
                    puts("Yes!");
                    return;
                }
        }
    }
    puts("No!");
}
int main() {
    int t = 1;
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    scanf("%d", &t);
    while(t--) {
        solve();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/gggyt/p/7670089.html