Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4797 | Accepted: 1998 |
Description
Input
Output
Sample Input
7 20 0 37 100 40 0 76 100 85 0 0 75 100 90 0 90 0 71 100 61 0 14 100 38 100 47 47 100 54.5 55.4
Sample Output
Number of doors = 2
Source
/************************************************************ * Author : kuangbin * Email : [email protected] * Last modified : 2013-07-14 21:59 * Filename : POJ1066TreasureHunt.cpp * Description : * *********************************************************/ #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <map> #include <vector> #include <set> #include <string> #include <math.h> using namespace std; const double eps = 1e-8; int sgn(double x) { if(fabs(x) < eps)return 0; if(x < 0)return -1; else return 1; } struct Point { double x,y; Point(){} Point(double _x,double _y) { x = _x;y = _y; } Point operator -(const Point &b)const { return Point(x - b.x,y - b.y); } //叉积 double operator ^(const Point &b)const { return x*b.y - y*b.x; } //点积 double operator *(const Point &b)const { return x*b.x + y*b.y; } //绕原点旋转角度B(弧度值),后x,y的变化 void transXY(double B) { double tx = x,ty = y; x = tx*cos(B) - ty*sin(B); y = tx*sin(B) + ty*cos(B); } }; struct Line { Point s,e; double k; Line(){} Line(Point _s,Point _e) { s = _s;e = _e; k = atan2(e.y - s.y,e.x - s.x); } //两条直线求交点, //第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交 //只有第一个值为2时,交点才有意义 pair<int,Point> operator &(const Line &b)const { Point res = s; if(sgn((s-e)^(b.s-b.e)) == 0) { if(sgn((s-b.e)^(b.s-b.e)) == 0) return make_pair(0,res);//重合 else return make_pair(1,res);//平行 } double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e)); res.x += (e.x-s.x)*t; res.y += (e.y-s.y)*t; return make_pair(2,res); } }; //两点间距离 double dist(Point a,Point b) { return sqrt((a-b)*(a-b)); } bool inter(Line l1,Line l2) { return max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) && max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) && max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) && max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) && sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s)) <= 0 && sgn((l1.s-l2.s)^(l2.e-l1.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= 0; } const int MAXN = 110; Line line[MAXN]; Point s; Point p[MAXN]; int main() { int n; while(scanf("%d",&n)==1) { double x1,y1,x2,y2; for(int i = 1;i <= n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); line[i] = Line(Point(x1,y1),Point(x2,y2)); p[2*i-1] = Point(x1,y1); p[2*i] = Point(x2,y2); } scanf("%lf%lf",&x1,&y1); s = Point(x1,y1); int ans = 1000000; for(int i = 1;i <= 2*n;i++) { int tmp = 0; Line line1 = Line(s,p[i]); for(int j = 1;j <= n;j++) if(inter(line1,line[j])) tmp++; ans = min(ans,tmp); } Line line1; line1 = Line(s,Point(0,0)); int tmp = 0; for(int i = 1;i <= n;i++) if(inter(line1,line[i])) tmp++; ans = min(ans,tmp+1); line1 = Line(s,Point(0,100)); tmp = 0; for(int i = 1;i <= n;i++) if(inter(line1,line[i])) tmp++; ans = min(ans,tmp+1); line1 = Line(s,Point(100,100)); tmp = 0; for(int i = 1;i <= n;i++) if(inter(line1,line[i])) tmp++; ans = min(ans,tmp+1); line1 = Line(s,Point(100,0)); tmp = 0; for(int i = 1;i <= n;i++) if(inter(line1,line[i])) tmp++; ans = min(ans,tmp+1); printf("Number of doors = %d\n",ans); } return 0; }