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

    Segments
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7363   Accepted: 2207

    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!

    Source

     
     
     
     
    #include<stdio.h>
    #include<math.h>
    
    const int eps=1e-8;
    
    struct point{
        double x,y;
    }p[410];
    
    double xmult(point a,point b,point c){
        return (a.x-b.x)*(c.y-b.y)-(c.x-b.x)*(a.y-b.y);
    }
    
    double abs(double a){
        if(a<0)
            return -a;
        return a;
    }
    
    bool work(int n){
        int flag;
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++){
                if(abs(p[j].x-p[i].x)>eps || abs(p[j].y-p[i].y)>eps){
                    flag=1;
                    for(int k=1;k<=n;k+=2)
                        if(xmult(p[i],p[j],p[k])*xmult(p[i],p[j],p[k+1])>0){
                            flag=0;
                            break;
                        }
                    if(flag)
                        return 1;
                }
            }
        return 0;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int t,n;
        scanf("%d",&t);
        while(t--){
            scanf("%d",&n);
            int cnt=1;
            for(int i=1;i<=n;i++,cnt+=2)
                scanf("%lf%lf%lf%lf",&p[cnt].x,&p[cnt].y,&p[cnt+1].x,&p[cnt+1].y);
            if(work(cnt-1))
                printf("Yes!\n");
            else
                printf("No!\n");
        }
        return 0;
    }
  • 相关阅读:
    面向对象:类的基础
    Apache:dbutils 开源JDBC工具类库
    JDBCToolsV3 :DAO
    JDBCToolsV2:利用ThreadLocal保证当前线程操作同一个数据库连接对象。
    JDBC: ThreadLocal 类
    JDBCTools 第一个版本
    数据库连接池
    JDBC:处理事务
    应变片安装
    博客园 PT100 烟温隔离板资料
  • 原文地址:https://www.cnblogs.com/jackge/p/2960434.html
Copyright © 2011-2022 走看看