Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15648 | Accepted: 4971 |
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!
题意就是问 是否有一条直线
使得所有线段在该线段上的投影有公共点
之后把问题转化为是不是存在直线使得它与所有的线段都有交点(在该共同区域做垂线,一定可以交)
所以枚举每两个个线段的端点就好了
#include<cmath> #include<ctime> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #include<iomanip> #include<vector> #include<string> #include<bitset> #include<queue> #include<map> #include<set> using namespace std; typedef double db; const int N=1010; const db eps=1e-8; int n; db a[N],b[N],c[N],d[N]; inline db xmul(db x,db y,db x2,db y2,db basx,db basy) {return (x-basx)*(y2-basy)-(x2-basx)*(y-basy);} inline bool check(db x,db y,db x2,db y2) { if(abs(x-x2)<eps&&abs(y-y2)<eps)return 0; for(int i=1;i<=n;++i) if(xmul(a[i],b[i],x,y,x2,y2)*xmul(c[i],d[i],x,y,x2,y2)>eps)return 0; return 1; } int main() { int T; register int i,j; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=1;i<=n;++i)scanf("%lf%lf%lf%lf",&a[i],&b[i],&c[i],&d[i]); bool flag=0; for(i=1;i<=n;++i) { for(j=1;j<=n;++j) { if(check(a[i],b[i],a[j],b[j])){flag=1;break;} if(check(a[i],b[i],c[j],d[j])){flag=1;break;} if(check(c[i],d[i],a[j],b[j])){flag=1;break;} if(check(c[i],d[i],c[j],d[j])){flag=1;break;} } if(flag)break; } flag?puts("Yes!"):puts("No!"); } return 0; } /* 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 Yes! Yes! No! */