首页 > 代码库 > zoj 1648 Circuit Board

zoj 1648 Circuit Board

题目:意思就是推断给定的几条线段是否有相交的。

方法:模版吧,有空在来细细学习。

代码:

#include <iostream>
#include <cstdio>
using namespace std;
struct   Point
{
    double   x,y;
};
struct   LineSeg
{
    Point   a,b;
};
double Cross(Point a, Point b, Point c )
{
    return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
int   Yu(LineSeg   u,LineSeg   v)
{
    return(   (max(u.a.x,u.b.x)>=min(v.a.x,v.b.x))&&
              (max(v.a.x,v.b.x)>=min(u.a.x,u.b.x))&&
              (max(u.a.y,u.b.y)>=min(v.a.y,v.b.y))&&
              (max(v.a.y,v.b.y)>=min(u.a.y,u.b.y))&&
              (Cross(v.a,u.b,u.a)*Cross(u.b,v.b,u.a)>=0)&&
              (Cross(u.a,v.b,v.a)*Cross(v.b,u.b,v.a)>=0));
}

int main()
{
    int n;
    int flag=0;
    LineSeg l[2002];
    while(cin>>n)
    {
        flag=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf%lf%lf",&l[i].a.x,&l[i].a.y,&l[i].b.x,&l[i].b.y);
            if(!flag)
            for(int j=0;j<i;j++)
            if(Yu(l[j],l[i]))
            {
                flag=1;
                break;
            }
        }
        if(!flag) cout<<"ok!"<<endl;
        else cout<<"burned!"<<endl;
    }
    return 0;
}



zoj 1648 Circuit Board