Poj 3304 Segments
来源:互联网 发布:cntk caffe 编辑:程序博客网 时间:2024/05/19 16:19
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 x1y1x2y2 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
321.0 2.0 3.0 4.04.0 5.0 6.0 7.030.0 0.0 0.0 1.00.0 1.0 0.0 2.01.0 1.0 2.0 1.030.0 0.0 0.0 1.00.0 2.0 0.0 3.01.0 1.0 2.0 1.0
Sample Output
Yes!Yes!No!
第一次看的时候 居然没看懂此题...QAQ
题意: 是否存在一条直线与所有线段都有交点
做法:每两条线段的各一个端点,比如线段AB 和线段CD 选取AC AD BC BD依次连成直线,再依次判断剩下的线段是否都和这条直线有交点。 用到了叉积来判断P3与P1P1的关系 因为x1y2+x3y1+x2y3-x3y2-x2y1-x1y3可能爆了 错了17次. 囍!
#include<iostream>#include<cmath>#include <stdio.h>#include <set>#include <map>#include <algorithm>using namespace std;#define MAX 150#define MIN 1e-8int n;struct Point{ double x,y;} Left[MAX],Right[MAX]; //端点存入数组double det(Point p1,Point p2,Point p3){ return (p1.x-p3.x)*(p2.y-p3.y)-(p2.x-p3.x)*(p1.y-p3.y);//判断点p3是否在线段p1p2的左右 // x1y2+x3y1+x2y3-x3y2-x2y1-x1y3 不要这样直接乘..可能会爆掉}int judge(Point p1,Point p2){ if(abs(p1.x-p2.x) < MIN && abs(p1.y-p2.y) < MIN) return 0; for(int i=0; i<n; i++) if(det(p1, p2, Left[i])*det(p1, p2, Right[i]) > MIN) return 0;//每个点都进行判断 return 1;}int main(){ int T,result; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%lf%lf%lf%lf",&Left[i].x,&Left[i].y,&Right[i].x,&Right[i].y); } result=0; if(n<3) result=true;//小于三条线段 肯定能够找到一条直线满足条件 for(int i=0; i<n; i++) { for(int j=i+1; j<n; j++) //从i+1开始 因为之前的情况已经枚举过 { if(result) break; if(judge(Left[i], Left[j])) result = 1; else if(judge(Left[i], Right[j])) result = 1; else if(judge(Right[i], Left[j])) result = 1; else if(judge(Right[i], Right[j])) result = 1; } if(result) break; } if(result) printf("Yes!\n"); else printf("No!\n"); } return 0;}
- POJ 3304 Segments
- poj 3304 Segments
- poj 3304 Segments
- poj 3304 Segments
- POJ 3304 Segments
- POJ 3304 Segments
- POJ 3304 Segments
- poj 3304 Segments
- poj 3304 Segments
- POJ 3304 Segments
- poj 3304 Segments
- POJ 3304 Segments
- poj 3304 ( segments)
- poj-3304-Segments
- POJ 3304 Segments
- POJ 3304 Segments
- poj 3304 Segments
- POJ 3304 Segments
- 经典问题之约瑟夫问题的快速解决
- error: insufficient permissions for device(解决adb shell问题)
- JavaEE学习笔记之SSH—Hibernate(2)
- Ubuntu14.04下eclipse中使用rohc库
- Java创建线程的两个方法
- Poj 3304 Segments
- 好的技术网站总站总结
- Xcode及常用插件安装
- 分析能力提高篇三:不懂装懂、无知者无畏的指手画脚,是大学正常发展的极大障碍
- hdu 1072 Nightmare (BFS)
- Hibernate One class to two table or Two class to one table
- List,Map,数组型Json生产以及解析
- From Android to iOS - 零基础编写一个iOS应用
- [HAXE] haxe接入IOS版FacebookSdK以及iOS开发遇到的问题总结(二)