题意:
有一个100大小的矩形房间,房间内有n堵墙;
每一堵墙都是横跨房间的,并且没有三个墙交于一点;
房间中的某个点有宝物,问从房间外面打破几道墙能到达宝物的地方;
题解:
思路十分神奇的一道题;
首先比较直观的思路就是建立对偶图跑最短路;
但是这我也不会写啊= =;
考虑到这些墙有一些特殊性——横跨房间;
就有了一种神奇的算法:
枚举房间外层的点,判断对于每一堵墙起点和终点是否在两边;
如果在两边,那么图中一定经过这堵墙!
思想是这样的,然后就是模拟水题了;
判断两边上叉积的乘积<0咯;
代码:
#include<math.h> #include<stdio.h> #include<string.h> #include<algorithm> #define N 33 using namespace std; const double EPS=1e-8; const double INF=1e100; struct Point { double x,y; Point(){} Point(double _,double __):x(_),y(__){} friend Point operator +(Point a,Point b) { return Point(a.x+b.x,a.y+b.y); } friend Point operator -(Point a,Point b) { return Point(a.x-b.x,a.y-b.y); } friend double operator *(Point a,Point b) { return a.x*b.x+a.y*b.y; } friend double operator ^(Point a,Point b) { return a.x*b.y-a.y*b.x; } }T; int top[4]; double st[4][N]; struct Line { Point p,v; void read() { static Point temp; scanf("%lf%lf%lf%lf",&p.x,&p.y,&temp.x,&temp.y); if(p.x==0) st[0][++top[0]]=p.y; if(p.x==100) st[1][++top[1]]=p.y; if(p.y==0) st[2][++top[2]]=p.x; if(p.y==100) st[3][++top[3]]=p.x; if(temp.x==0) st[0][++top[0]]=temp.y; if(temp.x==100) st[1][++top[1]]=temp.y; if(temp.y==0) st[2][++top[2]]=temp.x; if(temp.y==100) st[3][++top[3]]=temp.x; v=temp-p; } }l[N]; int main() { int n,m,i,j,k,x,y,ans,cnt; double p; scanf("%d",&n); for(i=1;i<=n;i++) l[i].read(); scanf("%lf%lf",&T.x,&T.y); for(i=0,ans=0x3f3f3f3f;i<4;i++) { st[i][++top[i]]=100; sort(st[i],st[i]+top[i]+1); for(j=1;j<=top[i];j++) { if(fabs(st[i][j]-st[i][j-1])<EPS) continue; p=(st[i][j]+st[i][j-1])/2; Point t; switch(i) { case 0:t=Point(0,p); break; case 1:t=Point(100,p); break; case 2:t=Point(p,0); break; case 3:t=Point(p,100); break; } for(k=1,cnt=0;k<=n&&cnt<ans;k++) { if((l[k].v^t-l[k].p)*(l[k].v^T-l[k].p)<-EPS) cnt++; } ans=min(ans,cnt); } } printf("Number of doors = %d\n",ans+1); return 0; }