Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16366 | Accepted: 5160 |
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.
题意:给出n条线段,问是否存在酱一条直线,使所有线段在直线上投影于一点.
思路:即是否存在一条直线经过所有线段,证明:若存在一条直线l和所有线段相交,作一条直线m和l垂直,则m就是题中要求的直线,所有线段投影的一个公共点即为垂足。
做法就是枚举任意两个端点,看看其组成的直线是否经过所有线段.证明:若一条直线经过所有线段,则可以通过旋转使其经过其中的两个端点.
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
const double esp = 1e-8;
const int ff = 0x3f3f3f3f;
struct node
{
double x,y;
node(double x = 0,double y = 0):x(x),y(y){}
};
struct line
{
node a,b;
}l[520];
int n;
double fork(node p0,node p1,node p2)
{
return (p0.x-p1.x)*(p2.y-p1.y)-(p2.x-p1.x)*(p0.y-p1.y);//计算向量叉乘
}
bool judge(node a,node b)
{
if(fabs(a.x - b.x)< esp&&fabs(a.y - b.y)< esp)//如果无法组成直线
return 0;
for(int i = 1;i<= n;i++)
{
if(fork(l[i].a,a,b)*fork(l[i].b,a,b)> 0)//如果线段两个点在直线一侧
return 0;
}
return 1;
}
void init()
{
mem(l,0);
}
int main()
{
int t;
cin>>t;
while(t--)
{
init();
scanf("%d",&n);
for(int i = 1;i<= n;i++)
scanf("%lf %lf %lf %lf",&l[i].a.x,&l[i].a.y,&l[i].b.x,&l[i].b.y);
int flag = 0;
for(int i = 1;i<= n;i++)
{
if(judge(l[i].a,l[i].b))//判断当前线段所在直线是否满足条件,开始没想到wa了
flag = 1;
for(int j = i+1;j<= n;j++)
{
if(judge(l[i].a,l[j].a)||judge(l[i].a,l[j].b)||judge(l[i].b,l[j].a)||judge(l[i].b,l[j].b))//
flag = 1;
}
}
if(flag)
cout<<"Yes!"<<endl;
else
cout<<"No!"<<endl;
}
return 0;
}