poj 1066 Treasure Hunt

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

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cmath>
  4 #include <cstdlib>
  5 #include <algorithm>
  6 #define maxn 500000
  7 using namespace std;
  8 
  9 const double eps=1e-10;
 10 const int inf=1<<30;
 11 int t1,t2,t3,t4,t5,c;
 12 
 13 int cmp(double x)
 14 {
 15     if(fabs(x)<eps) return 0;
 16     if(x>0) return 1;
 17     return -1;
 18 }
 19 
 20 struct point
 21 {
 22     double x,y;
 23     point() {}
 24     point(double a,double b):x(a),y(b) {}
 25     friend point operator -(const point &a,const point &b)
 26     {
 27         return point(a.x-b.x,a.y-b.y);
 28     }
 29     friend point operator *(const point &a,const double &b)
 30     {
 31         return point(a.x*b,a.y*b);
 32     }
 33     friend point operator /(const point &a,const double &b)
 34     {
 35         return point(a.x/b,a.y/b);
 36     }
 37 } p[maxn];
 38 point m[4][500000];
 39 
 40 int cmp1(const point &a,const point &b)
 41 {
 42     return a.x<b.x;
 43 }
 44 
 45 int cmp2(const point &a,const point &b)
 46 {
 47     return a.y<b.y;
 48 }
 49 
 50 struct line
 51 {
 52     point a,b;
 53     line() {}
 54     line(point x,point y):a(x),b(y) {};
 55 } ll[maxn];
 56 
 57 double det(const point &a,const point &b)
 58 {
 59     return a.x*b.y-a.y*b.x;
 60 }
 61 
 62 bool segment(point a1,point a2,point b1,point b2)
 63 {
 64     double c1=det(a2-a1,b1-a1),c2=det(a2-a1,b2-a1);
 65     double c3=det(b2-b1,a1-b1),c4=det(b2-b1,a2-b1);
 66     return cmp(c1)*cmp(c2)<0&&cmp(c3)*cmp(c4)<0;
 67 }
 68 
 69 void make(double x,double y)
 70 {
 71     if(y==0)
 72     {
 73         m[0][t1].x=x;
 74         m[0][t1++].y=y;
 75     }
 76     else if(y==100)
 77     {
 78         m[1][t2].x=x;
 79         m[1][t2++].y=y;
 80     }
 81     else if(x==100)
 82     {
 83         m[2][t3].x=x;
 84         m[2][t3++].y=y;
 85     }
 86     else if(x==0)
 87     {
 88         m[3][t4].x=x;
 89         m[3][t4++].y=y;
 90     }
 91 }
 92 
 93 void inti()
 94 {
 95     for(int i=1; i<=t1; i++)
 96     {
 97         p[c].x=(m[0][i].x+m[0][i-1].x)/2;
 98         p[c++].y=0;
 99     }
100     for(int i=1; i<=t3; i++)
101     {
102         p[c].y=(m[2][i].y+m[2][i-1].y)/2;
103         p[c++].x=100;
104     }
105     for(int i=1; i<=t2; i++)
106     {
107         p[c].x=(m[1][i].x+m[1][i-1].x)/2;
108         p[c++].y=100;
109     }
110     for(int i=1; i<=t4; i++)
111     {
112         p[c].y=(m[3][i].y+m[3][i-1].y)/2;
113         p[c++].x=0;
114     }
115 }
116 int main()
117 {
118     int T;
119     while(scanf("%d",&T)!=EOF)
120     {
121         int n=T;
122         double x1,y1,x2,y2;
123         t1=1,t2=1,t3=1,t4=1,t5=0;
124         c=0;
125         while(T--)
126         {
127             scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
128             make(x1,y1);
129             make(x2,y2);
130             point st(x1,y1);
131             point st1(x2,y2);
132             ll[t5].a=st;
133             ll[t5++].b=st1;
134         }
135         m[0][0].x=0;
136         m[0][0].y=0;
137         m[0][t1].x=100;
138         m[0][t1].y=0;
139         m[2][t3].x=100;
140         m[2][t3].y=100;
141         m[2][0].x=100;
142         m[2][0].y=0;
143         m[1][0].x=0;
144         m[1][0].y=100;
145         m[1][t2].x=100;
146         m[1][t2].y=100;
147         m[3][0].x=0;
148         m[3][0].y=0;
149         m[3][t4].x=0;
150         m[3][t4].y=100;
151         sort(m[0],m[0]+t1+1,cmp1);
152         sort(m[1],m[1]+t2+1,cmp1);
153         sort(m[2],m[2]+t3+1,cmp2);
154         sort(m[3],m[3]+t4+1,cmp2);
155         inti();
156         point pp,p1;
157         scanf("%lf%lf",&pp.x,&pp.y);
158         int ans=inf;
159         for(int i=0; i<c; i++)
160         {
161             line l1(p[i],pp);
162             int ans1=0;
163             for(int j=0; j<t5; j++)
164             {
165                 if(segment(l1.a,l1.b,ll[j].a,ll[j].b)) ans1++;
166             }
167             ans=min(ans,ans1);
168         }
169         if(n==0)
170         {
171             ans=0;
172         }
173         printf("Number of doors = %d
",ans+1);
174     }
175     return 0;
176 }
View Code
原文地址:https://www.cnblogs.com/fanminghui/p/3561157.html