标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12042 | Accepted: 3808 |
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!
可以理解成如果直线合法,然后旋转的话总是会和某两条直线的端点相交,枚举端点。找到一条合法的。
这题用规范相交就过了。
///判断直线与线段相交 ///做法:枚举每两个端点,要是存在一条直线经过这两个端点并且和所有线段相交就OK,但是不能为重合点. #include<stdio.h> #include<iostream> #include<string.h> #include<math.h> #include<algorithm> using namespace std; const double eps = 1e-8; const int N = 105; struct Point { double x,y; }; struct Line{ Point a,b; }line[N]; int n; double cross(Point a,Point b,Point c){ return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y); } double dis(Point a,Point b){ return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } bool isCross(Point a,Point b,Point c,Point d){ if(cross(a,b,c)*cross(a,b,d)>0) return true; return false; } bool check(Point a,Point b){ if(sqrt(dis(a,b))<eps) return false;//这里记得讨论 for(int i=0;i<n;i++){ if(isCross(a,b,line[i].a,line[i].b)){ return false; } } return true; } int main() { int tcase; scanf("%d",&tcase); while(tcase--) { scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%lf%lf%lf%lf",&line[i].a.x,&line[i].a.y,&line[i].b.x,&line[i].b.y); } if(n==1||n==2) { printf("Yes!\n"); continue; } bool flag=false; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(check(line[i].a,line[j].a)||check(line[i].b,line[j].b)||check(line[i].a,line[j].b)||check(line[i].b,line[j].a)){ flag = true; break; } } if(flag) break; } if(flag) printf("Yes!\n"); else printf("No!\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5448114.html