zoukankan      html  css  js  c++  java
  • POJ 3304 Segments 叉积

    题意:

    找出一条直线,让给出的n条线段在这条直线的投影至少有一个重合的点

    转化一下,以重合的点作垂线,那么这条直线一定经过那n条线段。现在就是求找到一条直线,让这条直线经过所有线段

    分析:

    假设存在这一条直线,我们以无穷远处作为支点,然后旋转,直到碰到一个线段的端点就停止旋转,此时还是穿过了所有线段;

    然后以这个端点为支点,旋转直线,直到碰到一个线段的端点就停止旋转,此时也穿过了所有线段。

    于是就证明了,如果直线与所有线段相交,那么必定存在一条直线经过线段中的两个端点

    那么,接下来枚举即可。注意要把重复的点去掉

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define eps 1e-8
    using namespace std;
    
    const int maxn=100+5;
    
    struct Point
    {
        double x,y;
        Point() {};
        Point(double xx,double yy)
        {
            x=xx;
            y=yy;
        }
    } pot[maxn*2];
    
    double crs_prdct(Point a,Point b)
    {
        return a.x*b.y-b.x*a.y;
    }
    
    Point sub(Point a,Point b)
    {
        return Point(a.x-b.x,a.y-b.y);
    }
    
    int main()
    {
    //    freopen("in.txt","r",stdin);
        int t,n;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            double x1,y1,x2,y2;
            for(int i=0; i<n; i++)
            {
                scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
                pot[2*i]=Point(x1,y1);
                pot[2*i+1]=Point(x2,y2);
            }
            bool flag;
            for(int i=0; i<2*n; i++)
            {
                for(int j=i+1; j<2*n; j++)
                {
                    if(pot[i].x==pot[j].x && pot[i].y==pot[j].y) continue;
                    flag=true;
                    for(int k=0; k<n; k++)
                    {
                        double tmp1=crs_prdct(sub(pot[2*k],pot[i]),sub(pot[2*k],pot[j]));
                        double tmp2=crs_prdct(sub(pot[2*k+1],pot[i]),sub(pot[2*k+1],pot[j]));
                        if(fabs(tmp1)<eps || fabs(tmp2)<eps || tmp1*tmp2<0) continue;
                        flag=false;
                        break;
                    }
                    if(flag) break;
                }
                if(flag) break;
            }
            printf("%s
    ",flag? "Yes!":"No!");
        }
        return 0;
    }
  • 相关阅读:
    华为机试题01背包问题
    丑数
    动态规划(1)
    Linux 后台启动 Redis
    redis.exceptions.ResponseError: MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk.
    SQLServer从渣仔到小白
    cmder 增强型命令行工具
    总结在部署分布式爬虫环境过程中常见的若干问题
    【pymongo.errors】Cursor not found
    浅析scrapy与scrapy_redis区别
  • 原文地址:https://www.cnblogs.com/pach/p/7208172.html
Copyright © 2011-2022 走看看