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 <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <cmath>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 5010
#define Mod 10001
using namespace std;
struct point
{
double x,y;
point(){}
point(double a,double b)
{
x=a;
y=b;
}
};
struct Line
{
point s,e;
Line(){}
Line(point a,point b)
{
s=a;
e=b;
}
};
Line line[MAXN];
double multy(point p0,point p1,point p2) //p0p1 x p0p2
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
bool inter(Line l1,Line l2)
{
return multy(l2.s,l1.s,l1.e)*multy(l2.e,l1.s,l1.e)<=0;
}
double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool check(Line a,int n)
{
if(dis(a.s,a.e)==0)
return false;
for(int i=0;i<n;++i)
if(inter(a,line[i])==false)
return false;
return true;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
double x1,y1,x2,y2;
for(int i=0;i<n;++i)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
line[i]=Line(point(x1,y1),point(x2,y2));
}
bool flag=false;
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
if(check(Line(line[i].s,line[j].s),n)||check(Line(line[i].s,line[j].e),n)||check(Line(line[i].e,line[j].s),n)||check(Line(line[i].e,line[j].e),n))
{
flag=true;
break;
}
if(flag)
puts("Yes!");
else
puts("No!");
}
return 0;
}