Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14290 | Accepted: 4557 |
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个线段,询问是否存在一条直线经过这些线段,即与这些线段都有公共点。
直接枚举所有端点构成的直线,是否存在直线满足条件,存在输出yes,否则输出no。
注意枚举点的时候要去掉距离小于1e-8的两点,否则答案不正确。
ac代码如下:
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<stdlib.h> using namespace std; const double eps = 1e-8; const double PI = acos(-1.0); int sgn(double x) { if(fabs(x) < eps)return 0; if(x < 0)return -1; else return 1; } struct Point { double x,y; Point() {} Point(double _x,double _y) { x = _x; y = _y; } Point operator -(const Point &b)const { return Point(x - b.x,y - b.y); } //叉积 double operator ^(const Point &b)const { return x*b.y - y*b.x; } //点积 double operator *(const Point &b)const { return x*b.x + y*b.y; } //绕原点旋转角度B(弧度值),后x,y的变化 void transXY(double B) { double tx = x,ty = y; x = tx*cos(B) - ty*sin(B); y = tx*sin(B) + ty*cos(B); } }; struct Line { Point s,e; Line() {} Line(Point _s,Point _e) { s = _s; e = _e; } //两直线相交求交点 //第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交 //只有第一个值为2时,交点才有意义 pair<int,Point> operator &(const Line &b)const { Point res = s; if(sgn((s-e)^(b.s-b.e)) == 0) { if(sgn((s-b.e)^(b.s-b.e)) == 0) return make_pair(0,res);//重合 else return make_pair(1,res);//平行 } double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e)); res.x += (e.x-s.x)*t; res.y += (e.y-s.y)*t; return make_pair(2,res); } }; double dist(Point a,Point b) { return sqrt((a-b)*(a-b)); } bool Seg_inter_line(Line l1,Line l2) //判断直线l1和线段l2是否相交 { return sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0; } int n; Line L[105]; bool fun2(Line l1) { if(dist(l1.s,l1.e)<eps) return false; int i; for(i=0;i<n;i++) if(!Seg_inter_line(l1,L[i])) return false; if(i==n) return true; } bool fun(Line l1,Line l2) { Line q; q.s=l1.s; q.e=l2.s; if(fun2(q)) return true; q.s=l1.e; q.e=l2.s; if(fun2(q)) return true; q.s=l1.s; q.e=l2.e; if(fun2(q)) return true; q.s=l1.e; q.e=l2.e; if(fun2(q)) return true; return false; } int main() { int t; scanf("%d",&t); while(t--) { int i,j; int flag=0; scanf("%d",&n); for(i=0;i<n;i++) scanf("%lf%lf%lf%lf",&L[i].s.x,&L[i].s.y,&L[i].e.x,&L[i].e.y); if(n<3) flag=1; else { for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(fun(L[i],L[j])) { flag=1; break; } } if(flag) break; } } if(flag) printf("Yes!\n"); else printf("No!\n"); } return 0; }
题目网址:点击打开链接http://poj.org/problem?id=3304