zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    MFC中添加ToolTip提示框
    神经网络算法程序
    DOS命令大全(经典收藏)
    axure团队合作开发原型图
    POJ 3233 Matrix Power Series(矩阵高速功率+二分法)
    了解你的家公家IP
    HDOJ 3518 Boring counting
    模板方法模式的房间改造-组合查询
    6最好的之一 HTML5/CSS3 演示(PPT)框架
    试想一下,在代码学习Swift!
  • 原文地址:https://www.cnblogs.com/gggyt/p/7670089.html
Copyright © 2011-2022 走看看