原题传送门:http://poj.org/problem?id=3304
博主的中文题面(数据水):
https://www.luogu.org/problemnew/show/T22834
Segments
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| < 1e-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!
题目大意
问是否有一条能与平面内所有线段相交的直线
题解
英文题面的直接意思并不是中文题面那样直接明了,具体的证明大家自己用几何画板做做垂线感性证明一下吧。。。因为一定能找到一条满足题意的直线使其过线段中的某两个端点(感性理解)。所以,我们只需要O(n^2)枚举所有端点,O(n)挨个检验这条直线与所有线段的交点即可水过。
代码
#include<cstdio>
#include<cstring>
#define db double
using namespace std;
const int M=105;
const db eps=1e-8;
struct pt{db x,y;};
struct li{pt f,t;};
db operator * (pt a,pt b){return a.x*b.y-a.y*b.x;}
pt operator - (pt a,pt b){return (pt){a.x-b.x,a.y-b.y};}
int sig(db a){return (a>eps)-(a<-eps);}
int n,top;
pt pp[M<<1];
li ll[M];
void in()
{
db a,b,c,d;
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
pp[++top]=(pt){a,b};
pp[++top]=(pt){c,d};
ll[i]=(li){pp[top],pp[top-1]};
}
}
db area(pt a,pt b,pt c){return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);}
int test(pt a,pt b,pt c,pt d)
{
int d1=sig(area(a,b,c));
int d2=sig(area(a,b,d));
if((d1^d2)==-2) return 1;
if(d1==0||d2==0) return 2;
return 0;
}
bool check(pt a,pt b)
{
if(!sig(a.x-b.x)&&!sig(a.y-b.y)) return 0;
for(int i=1;i<=n;++i)
if(test(a,b,ll[i].f,ll[i].t)==0) return 0;
return 1;
}
void ac()
{
bool flag=0;
for(int i=1;i<top;++i)
for(int j=i+1;j<=top;++j)
if(check(pp[i],pp[j])) {flag=1;break;}
if(flag) printf("Yes!\n");
else printf("No!\n");
// for(int i=1;i<=top;++i)
// printf("%lf %lf\n",pp[i].x,pp[i].y);
memset(pp,0,sizeof(pp));
memset(ll,0,sizeof(ll));
top=0;
}
int main()
{
int T;
scanf("%d",&T);
for(int i=1;i<=T;++i)
in(),ac();
return 0;
}