Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12512 | Accepted: 3977 |
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条线段,问是否存在一条直线经过所有线段
思路:假设有一条直线经过所有线段,我们可以先平移这条直线,使其与线段的一个端点相交,再旋转这条直线使其与线段的其他端点相交,所以只要存在直线,那么这条直线必然可以经过线段的两个端点.
所以可以枚举所有端点构成的直线,看这些直线中是否存在一条直线能经过所有线段即可
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> using namespace std; const double eps=1E-8; int n; struct Point { double x,y; Point (double a=0,double b=0):x(a),y(b) {} }; struct Line_segment //线段 { Point s,e; Line_segment() {} Line_segment(Point a,Point b):s(a),e(b) {} } tem; Line_segment p[201]; double multiply(Point sp,Point ep,Point op) { return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y)); } bool intersect_l(Line_segment u,Line_segment v) { return multiply(u.s,v.e,v.s)*multiply(v.e,u.e,v.s)>=-eps; } double Dist(Point a,Point b)//两点距离 { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } bool jjj(Point a,Point b)//判断过a和b的直线能否经过所有线段 { if(Dist(a,b)<eps) return 0; tem.s=a; tem.e=b; for(int i=0; i<n; i++) if(intersect_l(p[i],tem)==0) return false; return true; } int main() { int T,i,j,t; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=0; i<n; i++) scanf("%lf %lf %lf %lf",&p[i].s.x,&p[i].s.y,&p[i].e.x,&p[i].e.y); t=0; for(i=0; i<n; i++) for(j=i+1; j<n; j++) { if(jjj(p[i].s,p[j].s)||jjj(p[i].s,p[j].e)||jjj(p[i].e,p[j].s)||jjj(p[i].e,p[j].e)) { t=1; break; } } if(t==1||n==1) printf("Yes!\n"); else printf("No!\n"); } return 0; }