Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8639 | Accepted: 2631 |
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 numbersx1y1x2y2 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 andb 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
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=100+10;
int n;
const double eps=1e-8;
struct point
{
double x,y;
point()
{};
point(double _x,double _y)
{
x=_x;
y=_y;
}
};
struct line
{
point st,en;
line(){}
line(point _st,point _en)
{
st=_st;
en=_en;
}
}MyLine[MAXN];
double IsLeft(point a,point b,point c)
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
double GetDistance(point a,point b)
{
return sqrt(1.0*((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)));
}
bool Check(point a,point b)
{
if(GetDistance(a,b)<eps)
return false;
for(int i=0;i<n;i++)
{
if((IsLeft(a,b,MyLine[i].st)*IsLeft(a,b,MyLine[i].en))>eps)
return false;
}
return true;
}
int main ()
{
int cas;
double x1,y1,x2,y2;
cin>>cas;
while(cas--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
MyLine[i]=line(point(x1,y1),point(x2,y2));
}
bool flag=false;
if(1==n)
flag=true;
else
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(Check(MyLine[i].st,MyLine[i].en)||Check(MyLine[j].st,MyLine[i].en)
||Check(MyLine[i].st,MyLine[j].en)||Check(MyLine[j].st,MyLine[j].en)
||Check(MyLine[i].st,MyLine[j].st)||Check(MyLine[i].en,MyLine[j].en)
)
{
flag=true;
break;
}
}
if(flag)
break;
}
}
if(flag)
printf("Yes!\n");
else
printf("No!\n");
}
system("pause");
return 0;
}