判断是否存在一条直线穿过所有的线段
2014-10-15 12:29
423 查看
Segments
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 11 Accepted Submission(s) : 1
Problem 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个线段,然后判断是否存在一个直线,使所有的线段投影到该直线上之后形成的n个新的线段至少存在一个公共点,此意可以转化成该直线是否存在一条垂直线与所有的原线段都有交点;
分析:该垂直线一定经过n个线段上的其中两个端点,所以枚举n个线段的端点即可
程序:
#include"string.h" #include"stdio.h" #include"iostream" #include"algorithm" #include"queue" #include"stack" #define M 5009 #define N 100009 #include"stdlib.h" #include"math.h" #define inf 10000000000000000LL #define INF 0x3f3f3f3f const double PI=acos(-1.0); #define eps 1e-10 using namespace std; struct node { double x,y; node (){} node (double xx,double yy):x(xx),y(yy){} node operator -(node p) { return node (x-p.x,y-p.y); } double operator *(node p) { return x*p.y-y*p.x; } double operator ^(node p) { return x*p.x+y*p.y; } }p[12],q[12]; int cnt; struct line { node s,e; }l[111]; double max(double a,double b) { return a>b?a:b; } double min(double a,double b) { return a<b?a:b; } double cross(node a,node b,node c) { return (b-a)*(c-a); } double dot(node a,node b,node c) { return (b-a)^(c-a); } double len(node a) { return sqrt(a^a); } double dis(node a,node b) { return len(b-a); } int set_line_intersection(line l,line p) { if(dis(l.s,l.e)<eps)//当两个端点构成的直线长度是0的话return 0;因为该直线会与所有线段都有交点 return 0; else if(cross(l.s,l.e,p.s)*cross(l.s,l.e,p.e)<eps) return 1; else return 0; } int slove(int n) { if(n<=2) return 1; int i,j,k,flag; for(i=1;i<=n;i++) { flag=1; for(k=1;k<=n;k++) { if(!set_line_intersection(l[i],l[k])) { flag=0; break; } } if(flag) return 1; } for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { line l1; l1.s=l[i].s; l1.e=l[j].s; flag=1; for(k=1;k<=n;k++) { if(!set_line_intersection(l1,l[k])) { flag=0; break; } } if(flag) return 1; flag=1; l1.s=l[i].s; l1.e=l[j].e; for(k=1;k<=n;k++) { if(!set_line_intersection(l1,l[k])) { flag=0; break; } } if(flag) return 1; flag=1; l1.s=l[i].e; l1.e=l[j].s; for(k=1;k<=n;k++) { if(!set_line_intersection(l1,l[k])) { flag=0; break; } } if(flag) return 1; flag=1; l1.s=l[i].e; l1.e=l[j].e; for(k=1;k<=n;k++) { if(!set_line_intersection(l1,l[k])) { flag=0; break; } } if(flag) return 1; } } return 0; } int main() { int T,n,i; cin>>T; while(T--) { scanf("%d",&n); for(i=1;i<=n;i++) scanf("%lf%lf%lf%lf",&l[i].s.x,&l[i].s.y,&l[i].e.x,&l[i].e.y); if(slove(n)) printf("Yes!\n"); else printf("No!\n"); } return 0; }
相关文章推荐
- 判断是否存在一条直线穿过所有的线段
- poj 3304 Segments 【判断是否存在一条直线与所有线段相交】
- poj 3304 Segments 是否存在一条穿过所有线段的直线
- poj 3304 判断是否存在一条直线与所有线段相交
- poj3304 判断是否存在一条直线经过n条线段
- poj3304(是否存在一条直线与所有给出线段相交
- 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bccced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中
- 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。
- poj3304求是否存在一条直线可以与各个线段有交点
- 判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进
- poj3304-Segments 是否存在直线经过所有线段
- Interview Q&A - 在一个圆圈里有有限个点(数量一定为偶数),是否一定可以找到一条直线不穿过任意一个点并且把所有的点平分成两半?
- Segments----是否存在直线与所有线段相交
- poj 3304 找一条直线穿过所有线段
- poj 3304 判断是否有与所有线段相交的直线
- mysql判断一条记录是否存在,如果存在,则更新此语句,如果不存在,则插入
- 一条SQL语句 判断某个表中是否存在某个栏位!!
- 判断直线与线段是否相交,相交则输出交点x轴坐标
- 迷宫问题1-在给定的迷宫中判断是否存在一条路径从起点到终点
- POJ 3304 Segments (判断直线和线段是否相交)