Segments - POJ 3304 线段相交

来源:互联网 发布:免费收款收据软件 编辑:程序博客网 时间:2024/06/05 18:00

Segments
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10570 Accepted: 3281

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条线段,判断是否存在有一条直线,满足所有的线段在直线上投影后至少有一个公共点

思路:原命题等价为存在一条直线穿过所有的线段(易知过公共点且垂直于所求直线的直线符合条件,设为直线a),该命题又等价于从所有线段中任选两端点形成的直线存在可以穿过所有的线段的直线(可将a平移至一条线段端点,然后绕这点旋转,使a过另一条线段端点)

AC代码如下:

#include<cstdio>#include<cstring>using namespace std;struct Point{    double x,y;    Point(double x=0,double y=0):x(x),y(y){}}p[210];typedef Point Vector;double eps=1e-8;Vector operator + (Vector A,Vector B){return Point(A.x+B.x,A.y+B.y);}Vector operator - (Vector A,Vector B){return Point(A.x-B.x,A.y-B.y);}int dcmp(double x){return (x>eps)-(x<-eps);}double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}int SegmentInterSection(Point a1,Point a2,Point b1,Point b2){    int d1,d2;    d1=dcmp(Cross(a2-a1,b1-a1));    d2=dcmp(Cross(a2-a1,b2-a1));    if((d1^d2)==-2)      return 1;    if(d1==0 || d2==0)      return 2;    return 0;}int T,t,n,m;bool flag;bool test(Point a1,Point a2){    int i,j,k;    if(dcmp(a1.x-a2.x)==0 && dcmp(a1.y-a2.y)==0)      return 0;    for(i=1;i<=2*n;i+=2)       if(SegmentInterSection(a1,a2,p[i],p[i+1])==0)         return 0;    return 1;}void solve(){    int i,j;    for(i=1;i<=n*2;i++)       for(j=i+1;j<=n*2;j++)          if(test(p[i],p[j]))          {              flag=1;              return;          }}int main(){    int i,j,k;    scanf("%d",&T);    for(t=1;t<=T;t++)    {        scanf("%d",&n);        for(i=1;i<=n*2;i++)           scanf("%lf%lf",&p[i].x,&p[i].y);        flag=0;        solve();        if(flag)          printf("Yes!\n");        else          printf("No!\n");    }}



0 0
原创粉丝点击