首页 > 代码库 > POJ 3304 计算几何
POJ 3304 计算几何
Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9564 Accepted: 2943
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!
Source
Amirkabir University of Technology Local Contest 2006
<span style="color:#6600cc;">/************************************** author : Grant Yuan time : 2014/8/19 16:53 algorithm : 计算几何 source : POJ 3304 ***************************************/ #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #define MAX 107 #define eps 1e-8 #include<cmath> using namespace std; struct Point { double x,y; Point(){} Point(double _x,double _y) { x=_x; y=_y; } Point operator -(const Point&b) { return Point(x-b.x,y-b.y); } double operator *(const Point&b) { return x*b.x+y*b.y; } double operator ^(const Point&b) { return x*b.y-y*b.x; } bool operator ==(const Point&b) { return(x==b.x&&y==b.y); } }; struct Line { Point p1,p2; Line(){} Line(Point a,Point b){p1=a;p2=b;} }; Point P[2*MAX]; Line L[MAX]; bool check(Point q0,Point q1,Point q2,Point q3) { if(fabs(q0.x-q1.x)<eps&&fabs(q0.y-q1.y)<eps) return false; int t1=0,t2=0; if(((q1-q0)^(q2-q0))<-eps) t1=-1; else if(((q1-q0)^(q2-q0))>eps) t1=1; if(((q1-q0)^(q3-q0))<-eps) t2=-1; else if(((q1-q0)^(q3-q0))>eps) t2=1; if(t1*t2==-1||t1==0||t2==0) return true; return false; } int main() { int t; scanf("%d",&t); while(t--){ int n; scanf("%d",&n); for(int i=0;i<2*n;i++) { scanf("%lf%lf",&P[i].x,&P[i].y); } if(n==1||n==2) printf("Yes!\n"); else{ for(int i=0;i<2*n;i++) { if(i%2==0) L[i/2].p1=P[i]; else L[i/2].p2=P[i]; } Point q0,q1; bool ans=0; for(int i=0;i<2*n-1;i++) { q0=P[i]; for(int j=i+1;j<2*n;j++) { if(P[j].x==q0.x&&P[j].y==q0.y) continue; q1=P[j]; bool flag=1; for(int k=0;k<n;k++) { if(!check(q0,q1,L[k].p1,L[k].p2)){flag=0; break;} } if(flag) {ans=1;break;} } if(ans) break; } if(ans) printf("Yes!\n"); else printf("No!\n"); }} return 0; } </span>