POJ 3304 Segments[直线与线段相交]
Posted Candy?
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3304 Segments[直线与线段相交]相关的知识,希望对你有一定的参考价值。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13514 | Accepted: 4331 |
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!
Source
题目大意:给出n条线段两个端点的坐标,问所有线段投影到一条直线上,如果这些所有投影至少相交于一点就输出Yes!,否则输出No!。
暴力枚举线段的交点组成直线然后判相交就行了
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <vector> using namespace std; typedef long long ll; const int N=105; const double eps=1e-8; inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1; c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘; c=getchar();} return x*f; } inline int sgn(double x){ if(abs(x)<eps) return 0; else return x<0?-1:1; } struct Vector{ double x,y; Vector(double a=0,double b=0):x(a),y(b){} bool operator <(const Vector &a)const{ return x<a.x||(x==a.x&&y<a.y); } void print(){ printf("%lf %lf\n",x,y); } }; typedef Vector Point; Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);} Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);} Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);} Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);} bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;} double Cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; } double DisPP(Point a,Point b){ Point t=a-b; return sqrt(t.x*t.x+t.y*t.y); } struct Line{ Point s,t; Line(){} Line(Point p,Point v):s(p),t(v){} }a[N]; bool isLSI(Line l1,Line l2){ //puts("isLSI"); //l1.s.print();l1.t.print(); //l2.s.print();l2.t.print(); Vector v=l1.t-l1.s,u=l2.s-l1.s,w=l2.t-l1.s; //printf("%d %d end\n",sgn(Cross(v,u)),sgn(Cross(v,w))); return sgn(Cross(v,u))!=sgn(Cross(v,w))||!sgn(Cross(v,u))||!sgn(Cross(v,w)); } int n,m; double x,y,x2,y2; bool check(Line l){ if(sgn(DisPP(l.s,l.t))==0) return false; //printf("check\n"); //l.s.print();l.t.print(); for(int i=1;i<=n;i++) if(!isLSI(l,a[i])) return false; return true; } void solve(){ for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(check(Line(a[i].s,a[j].s))||check(Line(a[i].s,a[j].t)) ||check(Line(a[i].t,a[j].s))||check(Line(a[i].t,a[j].t))) {puts("Yes!");return;} puts("No!"); } int main(int argc, const char * argv[]) { int T=read(); while(T--){ n=read(); for(int i=1;i<=n;i++){ scanf("%lf%lf%lf%lf",&x,&y,&x2,&y2); a[i]=Line(Point(x,y),Point(x2,y2)); } solve(); } return 0; }
以上是关于POJ 3304 Segments[直线与线段相交]的主要内容,如果未能解决你的问题,请参考以下文章