hdu 2150

判断线段相交,但是自己的方法一直wa不知道为什么

看了题解过了

​

#include <iostream>
#include <cmath>
#include <stdio.h>

using namespace std;
const double eps = 1e-8;

int sgn(double x)
{
    if(fabs(x)<eps) return 0;
    if(x<0) return -1;
    else return 1;
}

struct Point
{
    long long x,y;
    int cont;
    Point(){}
    Point(long long _x,long long _y,int _cont=0)
    {
        x=_x;
        y=_y;
        cont=_cont;
    }
    Point operator -(const Point &b)const
    {
        return Point(x-b.x,y-b.y);
    }
    long long  operator ^(const Point &b)const
    {
        return x*b.y-y*b.x;
    }
    long long operator *(const Point &b)const
    {
        return x*b.x+y*b.y;
    }

};

struct Line
{
    Point s,e;
    int cont;
    Line(){}
    Line(Point _s,Point _e,int _cont=0)
    {
        s=_s;
        e=_e;
        cont=_cont;
    }
};

Line line[4000];
Point p[40][110];
int sum=0;
bool OnSeg(Line l1,Line l2)
{
    return
    ((l2.s-l1.s)^(l2.e-l1.s))*((l2.s-l1.e)^(l2.e-l1.e))<=0&&
    ((l1.s-l2.s)^(l1.e-l2.s))*((l1.s-l2.e)^(l1.e-l2.e))<=0;
}

/*bool inter(Line l1,Line l2) {
      return
      max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
      max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
      max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
      max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
      ((l2.s-l1.e)^(l1.s-l1.e))*((l2.e-l1.e)^(l1.s-l1.e)) <= 0 &&
      ((l1.s-l2.e)^(l2.s-l2.e))*((l1.e-l2.e)^(l2.s-l2.e)) <= 0;
}*/
int num[100];
int n;
bool judge()
{
   for(int i=0;i<n-1;i++)
   {
       for(int j=i+1;j<n;j++)
       {
           for(int z1=1;z1<num[i];z1++)
           {
               for(int z2=1;z2<num[j];z2++)
               {
                   Line l1 =Line(p[i][z1],p[i][z1-1]);
                   Line l2 =Line(p[j][z2],p[j][z2-1]);
                   if(OnSeg(l1,l2))
                        return true;
               }
           }
       }
   }
   return false;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&num[i]);
            for(int j=0;j<num[i];j++)
            {
                scanf("%lld %lld",&p[i][j].x,&p[i][j].y);
            }
        }
        if(judge())
        cout<<"Yes"<<endl;
        else
        cout<<"No"<<endl;
    }
    return 0;
}

​
#include <iostream>
#include <cmath>
#include <stdio.h>

using namespace std;
const double eps = 1e-8;

int sgn(double x)
{
    if(fabs(x)<eps) return 0;
    if(x<0) return -1;
    else return 1;
}

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

};

struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e)
    {
        s=_s;
        e=_e;
    }
};

Line line[3000];
Point p[3000];

bool OnSeg(Line l1,Line l2)
{
    return
    ((l2.s-l1.s)^(l2.e-l1.s))*((l2.s-l1.e)^(l2.e-l1.e))<=0&&
    ((l1.s-l2.s)^(l1.e-l2.s))*((l1.s-l2.e)^(l1.e-l2.e))<=0;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==1)
        {
            cout<<"Yes"<<endl;
            return 0;
        }
        int cont=0;
        int cont1=0;
        int fl=0;
        for(int i=0;i<n;i++)
        {
            if(fl==1)
                break;
            int m;
            scanf("%d",&m);
            cont=cont1;
            for(int j=0;j<m;j++)
            {
                scanf("%lf %lf",&p[j].x,&p[j].y);
                if(j>=1)
                {
                    line[cont1]=Line(p[j],p[j-1]);

                    if(i!=0)
                    {
                        for(int k=0;k<cont;k++)
                        {
                            if(OnSeg(line[cont1],line[k]))
                            {
                                fl=1;
                                break;
                            }
                        }
                    }
                    cont1++;
                }

            }
        }
        if(fl==0)
            cout<<"No"<<endl;
        else
            cout<<"Yes"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/leekerian/article/details/81103185