• hdu 3304(直线与线段相交)


    Segments
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12042   Accepted: 3808

    Description

    Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common.

    Input

    Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing a positive integer n ≤ 100 showing the number of segments. After that, n lines containing four real numbers x1 y1 x2 y2 follow, in which (x1, y1) and (x2, y2) are the coordinates of the two endpoints for one of the segments.

    Output

    For each test case, your program must output "Yes!", if a line with desired property exists and must output "No!" otherwise. You must assume that two floating point numbers a and b are equal if |a - b| < 10-8.

    Sample Input

    3
    2
    1.0 2.0 3.0 4.0
    4.0 5.0 6.0 7.0
    3
    0.0 0.0 0.0 1.0
    0.0 1.0 0.0 2.0
    1.0 1.0 2.0 1.0
    3
    0.0 0.0 0.0 1.0
    0.0 2.0 0.0 3.0
    1.0 1.0 2.0 1.0

    Sample Output

    Yes!
    Yes!
    No!
    可以理解成如果直线合法,然后旋转的话总是会和某两条直线的端点相交,枚举端点。找到一条合法的。
    这题用规范相交就过了。
    ///判断直线与线段相交
    ///做法:枚举每两个端点,要是存在一条直线经过这两个端点并且和所有线段相交就OK,但是不能为重合点.
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    const double eps = 1e-8;
    const int N = 105;
    struct Point
    {
        double x,y;
    };
    struct Line{
        Point a,b;
    }line[N];
    int n;
    
    double cross(Point a,Point b,Point c){
        return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
    }
    double dis(Point a,Point b){
        return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    }
    bool isCross(Point a,Point b,Point c,Point d){///这个函数判断直线和线段是否相交,直线是无线延长的,所以一个点在这一边一个点在那一边就OK
        if(cross(a,b,c)*cross(a,b,d)>0) return true;
        return false;
    }
    bool check(Point a,Point b){
        if(sqrt(dis(a,b))<eps) return false;//这里记得讨论
        for(int i=0;i<n;i++){
            if(isCross(a,b,line[i].a,line[i].b)){
                return false;
            }
        }
        return true;
    }
    int main()
    {
        int tcase;
        scanf("%d",&tcase);
        while(tcase--)
        {
            scanf("%d",&n);
            for(int i=0;i<n;i++){
                scanf("%lf%lf%lf%lf",&line[i].a.x,&line[i].a.y,&line[i].b.x,&line[i].b.y);
            }
             if(n==1||n==2) {
                printf("Yes!
    ");
                continue;
            }
            bool flag=false;
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    if(check(line[i].a,line[j].a)||check(line[i].b,line[j].b)||check(line[i].a,line[j].b)||check(line[i].b,line[j].a)){
                        flag = true;
                        break;
                    }
                }
                if(flag) break;
            }
            if(flag) printf("Yes!
    ");
            else printf("No!
    ");
        }
        return 0;
    }
  • 相关阅读:
    js在当前时间上加分钟数得到新的时间
    (转)@Autowired(required=false)注入注意的问题
    Java代替if和switch的方法(记录一下)
    windows下RocketMQ的安装部署
    RocketMQ在windows环境下的安装(转)
    简单说下二维数组
    JAVA-单例模式的几种实现方式
    (转)mybatis一级缓存二级缓存
    MySql安装后在服务管理器里边找不到MySql服务项的解决办法(win10)
    JAVA字符串的替换replace、replaceAll、replaceFirst的区别解析。
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5448114.html
Copyright © 2020-2023  润新知