Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11593 | Accepted: 3657 |
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
【题意】
给出n条线段两个端点的坐标,问所有线段投影到一条直线上,如果这些所有投影至少相交于一点就输出Yes!,否则输出No!。
【思路】
如果有存在这样的直线,过投影相交区域作直线的垂线,该垂线必定与每条线段相交,问题转化为问是否存在一条线和所有线段相交
若存在一条直线与所有线段相机相交,此时该直线必定经过这些线段的某两个端点,所以枚举任意两个端点即可
【代码】
#include<cmath>
#include<cstdio>
#include<cstring>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; const int N = +;
const double eps = 1e-; int dcmp(double x) {
if(x<eps) return ; else return x<? -:;
} struct Line {
double x1,y1,x2,y2;
}L[N]; int n; double cross(double x1,double y1,double x2,double y2,double x,double y) {
return (x2-x1)*(y-y1)-(x-x1)*(y2-y1);
}
bool judge(double x1,double y1,double x2,double y2) {
if(!dcmp(x2-x1) && !dcmp(y2-y1)) return ;
FOR(i,,n) {
if(cross(x1,y1,x2,y2,L[i].x1,L[i].y1)*
cross(x1,y1,x2,y2,L[i].x2,L[i].y2)>eps) return ;
}
return ;
} int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
FOR(i,,n)
scanf("%lf%lf%lf%lf",&L[i].x1,&L[i].y1,&L[i].x2,&L[i].y2);
if(n==) { puts("Yes!"); continue; }
bool ans=;
FOR(i,,n) {
FOR(j,i+,n) {
if(judge(L[i].x1,L[i].y1,L[j].x1,L[j].y1)) ans=;
if(judge(L[i].x1,L[i].y1,L[j].x2,L[j].y2)) ans=;
if(judge(L[i].x2,L[i].y2,L[j].x1,L[j].y1)) ans=;
if(judge(L[i].x2,L[i].y2,L[j].x2,L[j].y2)) ans=;
if(ans) break;
}
if(ans) break;
}
if(ans) puts("Yes!");
else puts("No!");
}
return ;
}