poj 1066 Treasure Hunt (未完)

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define eps 1e-8
#define zero(x) (((x)>0 ? (x):(-x))<eps)
#define INF 0x3f3f3f3f
struct Point
{
    double x,y;
}tre,point[80],tp;
struct Line
{
    Point a,b;
}line[40];
double xmult(Point p1,Point p2,Point p0)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double dmult(Point p1,Point p2,Point p0)
{
    return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);
}
int dis(Point p1,Point p2)
{
    return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
}
int cmp(const void *a,const void *b)
{
    Point *c=(Point *)a;
    Point *d=(Point *)b;
    int k=xmult(point[0],*c,*d);
    if(k<0||!k&&dis(point[0],*c)>dis(point[0],*d))
    return 1;
    return -1;
}
int main()
{
    int i,j,n,ans,tmp;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%lf%lf%lf%lf",&line[i].a.x,&line[i].a.y,&line[i].b.x,&line[i].b.y);
        point[2*i]=line[i].a;
        point[2*i+1]=line[i].b;
    }
    scanf("%lf%lf",&tre.x,&tre.y);

    int m=n;
    point[m].x=point[m++].y=0;
    point[m].x=0;point[m++].y=100;
    point[m].x=point[m++].y=100;
    point[m].x=100;point[m++].y=0;

    qsort(point+1,m-1,sizeof(Point),cmp);
    point[m++]=point[0];

    ans=INF;
    for(i=0;i<m-1;i++)
    {
        tmp=1;
        //tp.x=(point[i].x+point[i+1].x)/2;
        //tp.y=(point[i].y+point[i+1].y)/2;
        tp=point[i];
        for(j=0;j<n;j++)
        {
            if(xmult(tre,line[j].a,line[j].b)*xmult(tp,line[j].a,line[j].b)<-eps&&
               xmult(line[j].a,tre,tp)*xmult(line[j].b,tre,tp)<-eps)tmp++;
        }
        if(tmp<ans)ans=tmp;
    }
    printf("Number of doors = %d\n",ans-1);
    return 0;
}

 

你可能感兴趣的:(poj)