Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14264 | Accepted: 4546 |
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!
题意:给出n条直线,问存不存在一条直接使得n条直线在这条线上的投影相交于一点
思路:如果存在这样的直线,这条直线的垂线必然与其他直接都相交。枚举端点,判断直线是不是跟所有线段都相交。
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <math.h>
typedef long long ll;
using namespace std;
#define EPS 1e-8
double add(double a,double b)
{
if(fabs(a+b)<EPS*(fabs(a)+fabs(b))) return 0;
return a+b;
}
struct P
{
double x,y;
P(){}
P(double x,double y): x(x),y(y){}
P operator +(P p) {
return P(add(x,p.x),add(y,p.y));
}
P operator -(P p) {
return P(add(x,-p.x),add(y,-p.y));
}
P operator *(double d) {
return P(x*d,y*d);
}
P operator /(double d) {
return P(x/d,y/d);
}
bool operator == (P p) { //判断两个向量是否相等;
return x==p.x&&y==p.y;
}
double dot(P p) { //向量内积,内积等于0表示两条实现垂直
return add(x*p.x,y*p.y);
}
double det(P p) { //向量叉积,外积等于0表示在一条直线上
return add(x*p.y,-y*p.x);
}
}p[500];
bool on_seg(P p1,P p2,P q)//判断点q是否在线段p1,p2上
{
return (p1-q).det(p2-q)==0&&(p1-q).dot(p2-q)<=0;
}
P intersection(P p1,P p2,P q1,P q2)//计算直线p1-p2与q1-q2的交点
{
return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
}
bool is(P p1,P p2,P q1,P q2) //直线p1,q1与线段p2,q2的是否有交点
{
if((p1-q1).det(p2-q2)==0)
{
if(on_seg(p1,q1,p2)||on_seg(p1,q1,q2)||on_seg(p2,q2,p1)||on_seg(p2,q2,q1))
return true;
return false;
}
else
{
P p=intersection(p1,q1,p2,q2);
if(on_seg(p2,q2,p)) return true;
return false;
}
}
int main()
{
int n,m,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i+n].x,&p[i+n].y);
}
int flag=1;
for(int i=0;i<2*n;i++)
{
if(!flag) break;
for(int j=i+1;j<2*n;j++)
{
int ok=1;
for(int k=0;k<n;k++)
{
if(!is(p[i],p[k],p[j],p[k+n]))
{
ok=0;
break;
}
}
if(ok)
{
flag=0;
}
if(!flag) break;
}
}
if(!flag) printf("Yes!\n");
else printf("No!\n");
}
return 0;
}