#include<stdio.h>
#include<queue>
#include<cstring>
using namespace std;
const int N =4001;
struct Node{
int x,y,stp;
};
int m,n;
const int nx[4]={1,0,0,-1};
const int ny[4]={0,1,-1,0};
const int nex[8]={1,1,1,0,-1,-1,-1,0};
const int ney[8]={1,0,-1,-1,-1,0,1,1};
char a[N][N];
bool b[N][N];
bool c[N][N];
int main() {
scanf("%d%d",&m,&n);
for(int i=0;i<m;i++) {
scanf("%s",a[i]);
}
int sx,sy,ex,ey;
while(1) {
bool flag=false;
scanf("%d%d%d%d",&ex,&ey,&sx,&sy);ex--;ey--;sx--;sy--;
if(sx==-1) return 0;
queue<Node> Q;while(!Q.empty()) Q.pop();
memset(b,false,sizeof(b));
memset(c,false,sizeof(c));
for(int pos=0;pos<8;pos++) {
c[ex][ey]=true;
int x=ex , y=ey;
while(a[x][y]!='X' && x>=0 && y>=0 && x<m && y<n ) {
c[x][y]=true;
x+=nex[pos] , y+=ney[pos];
}
}
if(c[sx][sy]) {
printf("0\n");
continue;
}
Node start={sx,sy,0};
Q.push(start);
while(!Q.empty()) {
int x=Q.front().x , y=Q.front().y , stp=Q.front().stp;Q.pop();
if(c[x][y]) {
printf("%d\n",stp);while(!Q.empty()) Q.pop();
flag=true;
continue;
}
b[x][y]=true;
for(int pos=0;pos<4;pos++) {
int tx=x+nx[pos] , ty=y+ny[pos];
if(tx<0||ty<0||tx>=m||ty>=n||b[tx][ty]||a[tx][ty]=='X') continue;
Node next={tx,ty,stp+1};
Q.push(next);
}
}
if(!flag) printf("Poor Harry\n");
}
return 0;
}
提交记录