poj 1066 Treasure Hunt

时间:2023-12-30 19:11:26

http://poj.org/problem?id=1066

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define maxn 500000
using namespace std; const double eps=1e-;
const int inf=<<;
int t1,t2,t3,t4,t5,c; int cmp(double x)
{
if(fabs(x)<eps) return ;
if(x>) return ;
return -;
} struct point
{
double x,y;
point() {}
point(double a,double b):x(a),y(b) {}
friend point operator -(const point &a,const point &b)
{
return point(a.x-b.x,a.y-b.y);
}
friend point operator *(const point &a,const double &b)
{
return point(a.x*b,a.y*b);
}
friend point operator /(const point &a,const double &b)
{
return point(a.x/b,a.y/b);
}
} p[maxn];
point m[][]; int cmp1(const point &a,const point &b)
{
return a.x<b.x;
} int cmp2(const point &a,const point &b)
{
return a.y<b.y;
} struct line
{
point a,b;
line() {}
line(point x,point y):a(x),b(y) {};
} ll[maxn]; double det(const point &a,const point &b)
{
return a.x*b.y-a.y*b.x;
} bool segment(point a1,point a2,point b1,point b2)
{
double c1=det(a2-a1,b1-a1),c2=det(a2-a1,b2-a1);
double c3=det(b2-b1,a1-b1),c4=det(b2-b1,a2-b1);
return cmp(c1)*cmp(c2)<&&cmp(c3)*cmp(c4)<;
} void make(double x,double y)
{
if(y==)
{
m[][t1].x=x;
m[][t1++].y=y;
}
else if(y==)
{
m[][t2].x=x;
m[][t2++].y=y;
}
else if(x==)
{
m[][t3].x=x;
m[][t3++].y=y;
}
else if(x==)
{
m[][t4].x=x;
m[][t4++].y=y;
}
} void inti()
{
for(int i=; i<=t1; i++)
{
p[c].x=(m[][i].x+m[][i-].x)/;
p[c++].y=;
}
for(int i=; i<=t3; i++)
{
p[c].y=(m[][i].y+m[][i-].y)/;
p[c++].x=;
}
for(int i=; i<=t2; i++)
{
p[c].x=(m[][i].x+m[][i-].x)/;
p[c++].y=;
}
for(int i=; i<=t4; i++)
{
p[c].y=(m[][i].y+m[][i-].y)/;
p[c++].x=;
}
}
int main()
{
int T;
while(scanf("%d",&T)!=EOF)
{
int n=T;
double x1,y1,x2,y2;
t1=,t2=,t3=,t4=,t5=;
c=;
while(T--)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
make(x1,y1);
make(x2,y2);
point st(x1,y1);
point st1(x2,y2);
ll[t5].a=st;
ll[t5++].b=st1;
}
m[][].x=;
m[][].y=;
m[][t1].x=;
m[][t1].y=;
m[][t3].x=;
m[][t3].y=;
m[][].x=;
m[][].y=;
m[][].x=;
m[][].y=;
m[][t2].x=;
m[][t2].y=;
m[][].x=;
m[][].y=;
m[][t4].x=;
m[][t4].y=;
sort(m[],m[]+t1+,cmp1);
sort(m[],m[]+t2+,cmp1);
sort(m[],m[]+t3+,cmp2);
sort(m[],m[]+t4+,cmp2);
inti();
point pp,p1;
scanf("%lf%lf",&pp.x,&pp.y);
int ans=inf;
for(int i=; i<c; i++)
{
line l1(p[i],pp);
int ans1=;
for(int j=; j<t5; j++)
{
if(segment(l1.a,l1.b,ll[j].a,ll[j].b)) ans1++;
}
ans=min(ans,ans1);
}
if(n==)
{
ans=;
}
printf("Number of doors = %d\n",ans+);
}
return ;
}