POJ 3304 计算几何
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3703 | Accepted: 1026 |
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
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
Source Code
Problem: 3304 | User: bingshen | |
Memory: 200K | Time: 32MS | |
Language: C++ | Result: Accepted |
-
Source Code
#include<stdio.h> #include<math.h> #define eps 1e-8 struct point { double x; double y; }; struct line { point a; point b; }; point p[205]; line l[105]; double dis(point a,point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } double cross(point p1,point p2,point p0) { return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } bool work(int n) { int i,j,k; for(i=1;i<=2*n;i++) { for(j=i+1;j<=2*n;j++) { if(dis(p[i],p[j])<=eps) continue; for(k=1;k<=n;k++) if(cross(l[k].a,p[i],p[j])*cross(l[k].b,p[i],p[j])>0) break; if(k==n+1) return true; } } return false; } int main() { int i,n,t; double x,y; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%lf%lf",&x,&y); p[2*i-1].x=x; p[2*i-1].y=y; scanf("%lf%lf",&x,&y); p[2*i].x=x; p[2*i].y=y; l[i].a=p[2*i-1]; l[i].b=p[2*i]; } if(work(n)) printf("Yes!/n"); else printf("No!/n"); } return 0; }
推荐阅读