1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| const int MAX=16384+10; int n,m,fx,fy,ex,ey; char a[MAX]; int vis[MAX]; int aw[4][2]={-1,0,0,1,1,0,0,-1}; int aw2[8][2]={-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1}; struct node{ int x,y,c; node(int _x,int _y,int _c){ x=_x;y=_y;c=_c; } }; inline bool judge(int x,int y){ if(x<1||x>n||y<1||y>m) return false; return true; } inline void isok(int d){ int x,y; fi(i,0,8){ x=ex,y=ey; while(judge(x,y) && a[(x-1)*m+y-1]!='X'){ vis[(x-1)*m+y-1] = d; x += aw2[i][0]; y += aw2[i][1]; } } } int bfs(int d){ queue<node>q; q.push(node(fx,fy,0)); vis[(fx-1)*m+fy-1]=d; isok(d+1); if(vis[(fx-1)*m+fy-1] == d+1) return 0; while(!q.empty()){ node t = q.front(); q.pop(); fi(i,0,4){ int tx = t.x + aw[i][0],ty = t.y + aw[i][1]; if(judge(tx,ty) && vis[(tx-1)*m+ty-1]!=d && a[(tx-1)*m+ty-1]!='X'){ if(vis[(tx-1)*m+ty-1] == d+1) return t.c + 1; else { vis[(tx-1)*m+ty-1]= d ; q.push(node(tx,ty,t.c + 1)); } } } } return -1; } int main(){ freopen("in","r",stdin); mem(vis,0); sf(n);sf(m); fi(i,0,n) { scanf("%s",a+(i*m)); } int cnt=1; while(~sf(ex)&&ex){ sf(ey);sf(fx);sf(fy); int ans = bfs(cnt); cnt +=2; if(ans == -1) pfc("Poor Harry\n"); else pfn(ans); } return 0; }
|