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 integern ≤ 100 showing the number of segments. After that, n lines containing four real numbersx1 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 numbersa 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!
Hint
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define maxn 105
#define eps 1e-8
struct point
{
double x,y;
};
struct line
{
point sp,ep;
}L[maxn];
int n;
double Len_ab(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double x_multi(point p1,point p2,point p3)
{
return (p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y);
}
bool Cross(point p1,point p2,point p3,point p4) //判断直线(p1,p2)和线段(p3,p4)是否相交
{
double a=x_multi(p1,p2,p3);
double b=x_multi(p1,p2,p4);
if(fabs(a)<=eps) return true;
if(fabs(b)<=eps) return true;
if(a*b<=eps) return true;
return false;
}
bool OK(point a,point b)
{
if(Len_ab(a,b)<eps) //去重点
return false;
int i;
for(i=0;i<n;i++)
if(!Cross(a,b,L[i].sp,L[i].ep))
return false;
return true;
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%lf%lf%lf%lf",&L[i].sp.x,&L[i].sp.y,&L[i].ep.x,&L[i].ep.y);
if(n==1)
{
puts("Yes!");
continue;
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(OK(L[i].sp,L[j].sp)||OK(L[i].sp,L[j].ep)||OK(L[i].ep,L[j].sp)||OK(L[i].ep,L[j].ep))
{
puts("Yes!");
goto end;
}
}
puts("No!");
end:;
}
return 0;
}
本文介绍了一种算法,用于判断二维空间中是否存在一条直线能够与给定的所有线段相交。通过枚举线段端点并检查每一对端点确定的直线是否与所有线段有交点来实现。
2601

被折叠的 条评论
为什么被折叠?



