POJ 3304 Segment 直线与线段相交
2013-08-19 11:48
357 查看
C - Segments
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
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
x1y1x2y2 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
Sample Output
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
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
x1y1x2y2 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条线段在此直线的投影相交 思路: 考虑n的规模比较小,n3可以过,在考虑如存在一条直线,必然存在这条直线的一条垂线与n条线段相交,因此为题等价于求解是否存在一条直线与n条线段相交 如果存在这样一条直线,那么通过适当旋转,肯定使这条直线与n条线段的两个端点相交 枚举n条线段的两个端点,判断由两点组成的直线是否与n条线段相交 代码:
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<vector> using namespace std; #define eps 1e-10 #define maxn 110 const double pi=acos(-1.0); double fabs(double x) { return x<0?-x:x; } int equal(double x,double y) { return fabs(x-y)<eps; } double sqr(double x) { return x*x; } int cmp(double x) { return x<-eps?-1:x>eps; } struct point { double x,y; point() {} point(double a,double b):x(a), y(b){} void input() { scanf("%lf%lf",&x,&y);} friend point operator - (const point &a,const point &b) { return point(a.x-b.x,a.y-b.y); } friend point operator + (const point &a,const point &b) { return point(a.x+b.x,a.y+b.y); } friend point operator * (const point &a,const double r) { return point(a.x*r,a.y*r); } friend point operator / (const point &a,const double r) { return point(a.x/r,a.y/r); } friend int operator == (const point &a,const point &b) { return equal(a.x,b.x) && equal(a.y,b.y); } double norm(){ return sqrt(sqr(x)+sqr(y)); } }; double det(const point &a,const point &b) { return a.x*b.y-a.y*b.x; } double dot(const point &a,const point &b) { return a.x*b.x+a.y*b.y; } double dist(const point &a,const point &b) { return (a-b).norm(); } point rotate_point(const point &p,const double A) { double tx = p.x, ty = p.y; return point(tx*cos(A) -ty*sin(A), tx*sin(A)+ ty*cos(A)); } struct line { point a,b; line() {} line(point x,point y):a(x),b(y) {} line make_line(const point a,const point b) { return line(a,b); } double dis_point_segment(const point p,const point s,const point t) { if( cmp(dot(p-s,t-s))<=0) return (p-s).norm(); if( cmp(dot(p-t,s-t))<=0) return (p-t).norm(); return fabs( det(s-p,t-p)/dist(s,t)); } void PointProline(const point p,const point s,const point t,point &cp) { double r=dot(t-s,(p-s)/dot(t-s,t-s)); cp=s+(t-s)*r; } bool PointonSegment(const point p,const point s,const point t) { return cmp(det(p-s,t-s))==0&& cmp(dot(p-s,p-t))<=0; } bool parallel(line a,line b) { return !cmp(det(a.a-a.b,b.a-b.b)); } //直线 bool line_make_point(line a,line b,point &res) { if(parallel(a,b)) return false; double s1=det(a.a-b.a,b.b-b.a); double s2=det(a.b-b.a,b.b-b.a); res=(a.b*s1-a.a*s2)/(s1-s2); return true; } line move_d(line a,const double &len) { point d=a.b-a.a; d=d/d.norm(); d=rotate_point(d,pi/2); return line(a.a+d*len,a.b+d*len); } }; bool judge(line tmp,line t) { return cmp(det(t.a-tmp.a,tmp.b-tmp.a))*cmp(det(t.b-tmp.a,tmp.b-tmp.a))<=0; } line f[maxn]; point p[2*maxn]; int main() { int T; scanf("%d",&T); while(T--) { int n; int flag=0; scanf("%d",&n); for(int i=0;i<n;i++) f[i].a.input(),f[i].b.input(),p[i]=f[i].a,p[i+n]=f[i].b; for(int i=0;i<2*n;i++) { for(int j=i+1;j<2*n;j++) { if(dist(p[i],p[j])>eps){ int t=0; for(t=0;t<n;t++) { if(!judge(line(p[i],p[j]),f[t])) break; } if(n==t) { flag=1;break; } } } if(flag) break; } if(flag) puts("Yes!"); else puts("No!"); } return 0; }
相关文章推荐
- POJ 3304 Segment 直线与线段相交 + 枚举
- POJ 3304 Segment 直线与线段相交 + 枚举
- POJ 3304 Segment 直线与线段相交
- poj 3304 Segments(利用叉积求是否存在直线与所给线段相交)
- 判断直线与线段 是否相交 + 加入误差 故需要判断重点 poj 3304 Segments
- POJ 3304 segments (直线与线段相交)
- 【POJ - 3304 Segments】 直线线段相交判断
- 【线段直线相交】POJ 3304 Segments
- POJ 3304 Segments 判断直线与线段相交
- POJ 3304 Segments[直线与线段相交]
- poj 3304 Segments(判断线段和直线相交)
- poj 3304 Segments(判断直线与线段相交)
- POJ 3304 Segments (直线和线段相交判断)
- POJ 3304 Segments [判断线段和直线相交]
- POJ 3304 Segments (直线与线段相交)
- poj 3304线段与直线相交
- POJ 3304 Segments (计算几何、判断直线与线段是否相交)
- POJ 3304 Segments (直线和线段相交判断)
- POJ - 3304 :Segments__判断直线和线段是否 相交
- POJ 3304 Segments 判断直线和线段相交