poj3304 Segments

来源:互联网 发布:java短信流程 编辑:程序博客网 时间:2024/05/17 03:00
Segments
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9898 Accepted: 3050

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 (x1y1) and (x2y2) 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

321.0 2.0 3.0 4.04.0 5.0 6.0 7.030.0 0.0 0.0 1.00.0 1.0 0.0 2.01.0 1.0 2.0 1.030.0 0.0 0.0 1.00.0 2.0 0.0 3.01.0 1.0 2.0 1.0

Sample Output

Yes!Yes!

No!

题意:给出N条线段,问是否存在每条线段的投影交于一点的情况。

也就是说有一条直线经过所有的线段。

题解:要经过这N条线段,通过平移,满足条件,这条直线肯定经过这些线段的至少某两个端点。所以枚举这2*N个端点,看是否这一条直线与这N条线段相交。判断直线与线段相交,可以把线段的两个端点跟直线的位置进行叉积。分别在直线两边(叉积为负)或者某个端点在直线上(叉积为零)则线段与直线相交。

#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <iostream>#include <cmath>#include <queue>#include <map>#include <stack>#include <list>#include <vector>#include <ctime>#define LL __int64#define eps 1e-8using namespace std;struct point{double x,y;}l[200],r[200],p[210];double across(point a,point b,point c){return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);}int go(point a,point b,point c,point d){double ans=across(a,b,c)*across(a,b,d);if (ans<0 || fabs(ans)<eps) return 1;else return 0;}int main(){int T,i,j,k,n;scanf("%d",&T);while (T--){scanf("%d",&n);int cnt=0;for (i=1;i<=n;i++){scanf("%lf%lf%lf%lf",&l[i].x,&l[i].y,&r[i].x,&r[i].y);p[cnt].x=l[i].x;p[cnt++].y=l[i].y;p[cnt].x=r[i].x;p[cnt++].y=r[i].y;}int flag=1;for (i=0;i<cnt && flag;i++){for (j=i+1;j<cnt && flag;j++){if (p[i].x==p[j].x && p[i].y==p[j].y) continue;int ff=1;for (k=1;k<=n;k++){if (!go(p[i],p[j],l[k],r[k])){ff=0;break;}}if (ff) flag=0;}}if (flag) cout<<"No!"<<endl;else cout<<"Yes!"<<endl;}return 0;}


0 0
原创粉丝点击