zoukankan      html  css  js  c++  java
  • POJ 3304 Segments(判断直线和线段相交)

    Segments
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12150   Accepted: 3845

    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 (x1y1) and (x2y2) 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


    原博:http://blog.csdn.net/wangjian8006

    题目大意:给出n条线段两个端点的坐标,问所有线段投影到一条直线上,如果这些所有投影至少相交于一点就输出Yes!,否则输出No!。
    解题思路:如果有存在这样的直线,过投影相交区域作直线的垂线,该垂线必定与每条线段相交,问题转化为问是否存在一条线和所有线段相交
    若存在一条直线与所有线段相机相交,此时该直线必定经过这些线段的某两个端点,所以枚举任意两个端点即可。
    这里要主要的地方就是,题目说如果两个点的距离小于1e-8就等价于一点,所以要考虑重点

    /*
    Memory 200K
    Time   32MS 
    */
    #include <iostream>
    #include <math.h>
    using namespace std;
    #define MAXM 110
    #define EPS 1e-8
    
    typedef struct{
    	double x1,y1,x2,y2;
    }Segment;
    
    Segment segment[MAXM];
    int n;
    
    double distance(double x1,double y1,double x2,double y2){
    	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    }
    
    double corss(double x1,double y1,double x2,double y2,double x,double y){
    	return (x2-x1)*(y-y1)-(x-x1)*(y2-y1);
    }
    
    bool judge(double x1,double y1,double x2,double y2){
    	int i;
    	if(distance(x1,y1,x2,y2)<EPS) return 0;
    	for(i=0;i<n;i++){
    		if(corss(x1,y1,x2,y2,segment[i].x1,segment[i].y1)*
    			corss(x1,y1,x2,y2,segment[i].x2,segment[i].y2)>EPS) return 0;
    	}
    	return 1;
    }
    
    int main(){
    	int t,i,j,ans;
    	scanf("%d",&t);
    	while(t--){
    		scanf("%d",&n);
    		for(i=0;i<n;i++)
    			scanf("%lf%lf%lf%lf",&segment[i].x1,&segment[i].y1,&segment[i].x2,&segment[i].y2);
    		if(n==1) {printf("Yes!
    ");continue;}
    
    		ans=0;
    		for(i=0;i<n && !ans;i++)
    			for(j=i+1;j<n && !ans;j++){
    				if(judge(segment[i].x1,segment[i].y1,segment[j].x1,segment[j].y1) ||
    					judge(segment[i].x1,segment[i].y1,segment[j].x2,segment[j].y2) ||
    					judge(segment[i].x2,segment[i].y2,segment[j].x1,segment[j].y1) ||
    					judge(segment[i].x2,segment[i].y2,segment[j].x2,segment[j].y2))
    					ans=1;
    			}
    		if(ans) printf("Yes!
    ");
    		else printf("No!
    ");
    	}
    	return 0;




  • 相关阅读:
    android通过Canvas和Paint截取无锯齿圆形图片
    【转】mysql的cardinality异常,导致索引不可用
    mysql索引无效且sending data耗时巨大原因分析
    linux shell脚本通过参数名传递参数值
    git日志输出格式及两个版本之间差异列表
    jenkins结合ansible用shell实现自动化部署和回滚
    Linux下cp -rf总是提示覆盖的解决办法
    jenkins集成ansible注意事项Failed to connect to the host via ssh.
    ansible操作远程服务器报Error: ansible requires the stdlib json or simplejson module, neither was found!
    利用ssh-copy-id无需密码登录远程服务器
  • 原文地址:https://www.cnblogs.com/zswbky/p/6717940.html
Copyright © 2011-2022 走看看