POJ 1066 Treasure Hunt

 蒻苣:弱弱很弱,路过的巨巨还不吝赐教!^.^...QAQ

题意:给出n条线段,和一个100*100的格子,然后给出这n条线段在格子四条边上的坐标,再给出方格内的一点,求的这个人至少要经过几条线段能够走出方格,方格的边也算作一条线段。

题解:枚举每一个在边上的点,连接其与n条线段相交的个数,取个数最小的点。再+1就是答案

代码:

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <cmath>
 8 using namespace std;
 9 const double INF  = 1E200;
10 const double wc = 1E-8;
11 struct POINT{
12         double x,y;
13         POINT( double a = 0,double b = 0 )  { x = a; y = b; }
14 };
15 POINT p[10000+50];
16 struct LINESEG{
17         POINT e;
18         POINT s;
19         LINESEG( POINT x,POINT y ) { e = x; s = y; }
20         LINESEG(){}
21 };
22 LINESEG L[100000+10];
23 double dist(POINT p1,POINT p2)
24 {
25  return( sqrt( (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y) ) );
26 }
27 double multiply(POINT sp,POINT ep,POINT op)
28 {
29  return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));
30 }
31 bool online(LINESEG l,POINT p)
32 {
33  return( (multiply(l.e,p,l.s)==0) &&( ( (p.x-l.s.x)*(p.x-l.e.x)<=0 )&&( (p.y-l.s.y)*(p.y-l.e.y)<=0 ) ) );
34 }
35 bool intersect(LINESEG u,LINESEG v)
36 {
37  return( (max(u.s.x,u.e.x)>=min(v.s.x,v.e.x))&&
38    (max(v.s.x,v.e.x)>=min(u.s.x,u.e.x))&&
39    (max(u.s.y,u.e.y)>=min(v.s.y,v.e.y))&&
40    (max(v.s.y,v.e.y)>=min(u.s.y,u.e.y))&&
41    (multiply(v.s,u.e,u.s)*multiply(u.e,v.e,u.s)>=wc)&&
42    (multiply(u.s,v.e,v.s)*multiply(v.e,u.e,v.s)>=wc));
43 }
44 bool intersect_A(LINESEG u,LINESEG v)
45 {
46  return ((intersect(u,v))&&
47    (!online(u,v.s))&&
48    (!online(u,v.e))&&
49    (!online(v,u.e))&&
50    (!online(v,u.s)));
51 }
52 bool intersect_l(LINESEG u,LINESEG v)
53 {
54  return multiply(u.s,v.e,v.s)*multiply(v.e,u.e,v.s)>=wc;
55 }
56 int vis[100000+50];
57 int main()
58 {
59         int n;
60         cin>>n;
61         int len;
62         for(int j=0, i = 0; i < n; i++)
63         {
64                 scanf("%lf%lf",&p[j].x,&p[j].y);
65                 L[i].e = p[j]; j++;
66                 scanf("%lf%lf",&p[j].x,&p[j].y);
67                 L[i].s = p[j]; j++;
68                 len = j;
69         }
70         POINT a;
71         scanf("%lf%lf",&a.x,&a.y);
72         if(n==0) { printf("Number of doors = 1\n");return 0;}
73         int mx=9999999;
74         for(int i = 0;i<len;i++)
75         {
76                 LINESEG l;
77                 l.e = p[i];
78                 l.s = a;
79                 int ans = 0;
80                 for(int j = 0;j<n;j++)
81                 {
82                         if(intersect(l,L[j]))
83                         {
84                                 ans++;
85                         }
86                 }
87                 if(ans<mx)
88                         mx=ans;
89         }
90         printf("Number of doors = ");
91         cout<<mx+1<<endl;
92 }
View Code

 

posted on 2015-08-21 23:20  小松song  阅读(104)  评论(0)    收藏  举报

导航