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!");
    	}
    }
    
    
    
    


  • 相关阅读:
    排它锁、意向排它锁
    8.3锁定
    Cocos2d-x游戏引擎实战开发炸弹超人项目教程 全套下载 1至6课
    springboot加载yml配置文件的no字段自动转义问题
    动态规划——LeetCode152乘积最大子序列
    动态规划——LeetCode279完全平方数
    动态规划——LeetCode221最大正方形
    动态规划——LeetCode322零钱兑换(最少硬币数量)
    动态规划——LeetCode264丑数 II
    动态规划——LeetCode213打家劫舍 II
  • 原文地址:https://www.cnblogs.com/suika/p/9017745.html
Copyright © 2011-2022 走看看