首页 > 其他 > 详细

POJ 3304 计算几何

时间:2014-08-19 19:12:15      阅读:341      评论:0      收藏:0      [点我收藏+]

Segments
Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 9564  Accepted: 2943

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

Amirkabir University of Technology Local Contest 2006

 

<span style="color:#6600cc;">/**************************************
     author    : Grant Yuan
     time      : 2014/8/19 16:53
     algorithm : 计算几何
     source    : POJ 3304
***************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define MAX 107
#define eps 1e-8
#include<cmath>

using namespace std;

struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x=_x;
        y=_y;
    }
    Point operator -(const Point&b)
    {
        return Point(x-b.x,y-b.y);
    }
    double operator *(const Point&b)
    {
       return x*b.x+y*b.y;
    }
    double operator ^(const Point&b)
    {
        return x*b.y-y*b.x;
    }
    bool operator ==(const Point&b)
    {
        return(x==b.x&&y==b.y);
    }
};

struct Line
{
    Point p1,p2;
    Line(){}
    Line(Point a,Point b){p1=a;p2=b;}
};

Point P[2*MAX];
Line L[MAX];

bool check(Point q0,Point q1,Point q2,Point q3)
{
    if(fabs(q0.x-q1.x)<eps&&fabs(q0.y-q1.y)<eps)
        return false;
    int t1=0,t2=0;
    if(((q1-q0)^(q2-q0))<-eps) t1=-1;
    else if(((q1-q0)^(q2-q0))>eps) t1=1;

     if(((q1-q0)^(q3-q0))<-eps) t2=-1;
    else if(((q1-q0)^(q3-q0))>eps) t2=1;
    if(t1*t2==-1||t1==0||t2==0)
        return true;
    return false;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
      int n;
      scanf("%d",&n);
      for(int i=0;i<2*n;i++)
      {
          scanf("%lf%lf",&P[i].x,&P[i].y);
      }
      if(n==1||n==2) printf("Yes!\n");
      else{
      for(int i=0;i<2*n;i++)
      {
          if(i%2==0) L[i/2].p1=P[i];
          else L[i/2].p2=P[i];
      }
      Point q0,q1;
      bool ans=0;
      for(int i=0;i<2*n-1;i++)
      {
        q0=P[i];
        for(int j=i+1;j<2*n;j++)
        {
        if(P[j].x==q0.x&&P[j].y==q0.y) continue;
        q1=P[j];
        bool flag=1;
        for(int k=0;k<n;k++)
        {
            if(!check(q0,q1,L[k].p1,L[k].p2)){flag=0; break;}
        }
        if(flag) {ans=1;break;}
        }
        if(ans) break;
      }
       if(ans) printf("Yes!\n");
      else printf("No!\n");
    }}
    return 0;
}
</span>


 

POJ 3304 计算几何,布布扣,bubuko.com

POJ 3304 计算几何

原文:http://blog.csdn.net/yuanchang_best/article/details/38684593

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!