题意:从给定点到边框外,至少要经过多少根线,不能从交点经过。。
思路:连接给定点与边框上的任意一点,求与这边相交的最小边数。因为给出的线与边框的交点都是整数点,所以只要判断每一个点的左右两边附近的两个点就行了。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> using namespace std; const double EPS = 1e-6; struct cvector{ double x,y; cvector(double a,double b){x=a,y=b;} cvector(){} }; cvector operator+(cvector a,cvector b){ return cvector(a.x+b.x,a.y+b.y); } cvector operator-(cvector a,cvector b){ return cvector(a.x-b.x,a.y-b.y); } cvector operator*(double a,cvector b){ return cvector(a*b.x,a*b.y); } double operator*(cvector a,cvector b){ return a.x*b.x+a.y*b.y; } double operator^(cvector a,cvector b){ return a.x*b.y-b.x*a.y; } double length(double t){return t>0?t:-t;} double length(cvector t){return sqrt(t*t);} struct cpoint{ double x,y; cpoint(double a,double b){x=a,y=b;} cpoint(){} }; cvector operator-(cpoint a,cpoint b) { return cvector(b.x-a.x,b.y-a.y); } double dist(cpoint a,cpoint b){ return length(b-a); } struct cline{ cpoint a,b; }; bool intersect(cline a,cline b) { return ((a.a-b.a)^(b.b-b.a))*((a.b-b.a)^(b.b-b.a))<EPS; } cline re[39]; cpoint p[69]; bool operator <(cpoint a,cpoint b) { if(a.x==b.x) return a.y<b.y; return a.x<b.x; } int n; int oor(cline tmp) { int ans =0; for(int i=0;i<n;i++) if(intersect(tmp,re[i])) ans++; return ans; } int main() { freopen("in.txt","r",stdin); scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%lf%lf%lf%lf",&re[i].a.x,&re[i].a.y,&re[i].b.x,&re[i].b.y); p[i<<1].x=re[i].a.x,p[i<<1].y=re[i].a.y; p[i<<1|1].x=re[i].b.x,p[i<<1|1].y=re[i].b.y; } cline tmp; int ans = 0x3f3f3f3f; scanf("%lf%lf",&tmp.a.x,&tmp.a.y); if(n==0) { ans=0; } for(int i=0;i<(n<<1);i++) { if(p[i].x==0||p[i].x==100) { tmp.b.x=p[i].x,tmp.b.y=p[i].y+0.5; ans = min(ans,oor(tmp)); tmp.b.x=p[i].x,tmp.b.y=p[i].y-0.5; ans = min(ans,oor(tmp)); } if(p[i].y==0||p[i].y==100) { tmp.b.x=p[i].x+0.5,tmp.b.y=p[i].y; ans = min(ans,oor(tmp)); tmp.b.x=p[i].x-0.5,tmp.b.y=p[i].y; ans = min(ans,oor(tmp)); } } printf("Number of doors = %d\n",ans+1); return 0; }