zoukankan      html  css  js  c++  java
  • POJ_3304_Segments_线段判断是否相交

    POJ_3304_Segments_线段判断是否相交

    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!


    在二维空间中给定n个线段,写一个程序,它决定是否存在一条线,这样在投射出这些段之后,所有的投影段至少有一个点是相同的。

    假设存在这样的直线,那么过所有的投影的交点做这条直线的垂线一定和所有线段相交,问题转化为是否存在一条直线与所有线段相交。
    并且这条线段能通过平移或旋转使它经过某两个线段的端点。于是只需要把所有点存起来,枚举直线。
    注意这道题可能有重点。

    代码:
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    typedef double f2;
    #define eps 1e-8
    struct Point {
    	f2 x,y;
    	Point() {}
    	Point(f2 x_,f2 y_) :
    		x(x_),y(y_) {}
    	Point operator + (const Point &p) {return Point(x+p.x,y+p.y);}
    	Point operator - (const Point &p) {return Point(x-p.x,y-p.y);}
    	Point operator * (f2 rate) {return Point(x*rate,y*rate);}
    };
    f2 dot(const Point &p1,const Point &p2) {return p1.x*p2.x+p1.y*p2.y;}
    f2 cross(const Point &p1,const Point &p2) {return p1.x*p2.y-p1.y*p2.x;}
    f2 fabs(f2 x) {return x>0?x:-x;}
    int n;
    Point a[120],b[120],c[220],p1,p2;
    bool judge(f2 x,f2 y) {
    	if(fabs(x)<eps||fabs(y)<eps) return 1;
    	return (x<-eps&&y>eps)||(x>eps&&y<-eps);
    }
    bool check() {
    	int i;
    	Point t=p1-p2;
    	for(i=1;i<=n;i++) {
    		Point d1=a[i]-p2,d2=b[i]-p2;
    		f2 tmp1=cross(t,d1),tmp2=cross(t,d2);
    		if(!judge(tmp1,tmp2)) return 0;
    	}
    	return 1;
    }
    int main() {
    	int T;
    	scanf("%d",&T);
    	while(T--) {
    		int flg=0;
    		scanf("%d",&n);
    		int i,j;
    		for(i=1;i<=n;i++) {
    			scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&b[i].x,&b[i].y);
    			c[i]=a[i];
    			c[i+n]=b[i];
    		}
    		for(i=1;i<=2*n;i++) {
    			for(j=i+1;j<=2*n;j++) {
    				if(fabs(c[i].x-c[j].x)>eps||fabs(c[i].y-c[j].y)>eps) {
    					p1=c[i]; p2=c[j];
    					if(check()) {
    						flg=1; break;
    					}
    				}
    			}
    			if(flg) break;
    		}
    		puts(flg?"Yes!":"No!");
    	}
    }
    
    
    
    


  • 相关阅读:
    Mego(02)
    Mego(01)
    ThoughtWorks(中国)程序员读书雷达 —— 书籍下载整理
    Spring源码编译一次性通过&遇到的坑解决方法
    Elasticsearch怎么修改索引字段类型?
    Flume 自定义拦截器 多行读取日志+截断
    用Hibernate框架把hql生成可执行的sql语句-Oracle方言
    深入浅出SQL Server中的死锁 [转于CareySon]
    第一次迭代随笔
    结对编程代码分析
  • 原文地址:https://www.cnblogs.com/suika/p/9017745.html
Copyright © 2011-2022 走看看