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


  • 相关阅读:
    Spring Boot 2 (四):使用 Docker 部署 Spring Boot
    Spring Boot 2 (三):Spring Boot 2 相关开源软件
    ConnectionString属性(网速慢的情况下研究Connect Timeout)
    Flexbox布局入门笔记
    ASP.Net一般处理程序Session用法
    AngularJS 插值字符串 $interpolate
    浅谈AngularJS的$parse服务
    VsCode 解决vim插件,不能使用ctrl+c的问题(目标卸载im)
    win10远程桌面 CredSSP加密Oracle修正的解决办法
    fatal:'origin' does not appear to be a git repository fatal:Could not read from remote repository
  • 原文地址:https://www.cnblogs.com/suika/p/9017745.html
Copyright © 2011-2022 走看看