洛谷P2199 70分WA
#include
using namespace std;
int dx[5]= {1,-1,0,0};
int dy[5]= {0,0,1,-1};
int nx,ny;
char a[10085][10085];
bool vis[10085][10085];
struct node {
int x;
int y;
int step;
} b[16385];
int sx,sy,ex,ey;
int n,m;
int fx[10]= {1,-1,0,0,1,-1,-1,1};
int fy[10]= {0,0,1,-1,1,-1,1,-1};
bool check(int xx, int yy) {
if (xx==ex&&yy==ey) return true;
for (int i=0; i<=7; i++) {
int nx=xx,ny=yy;
while(nx>=1&&nx<=n&&ny>=1&&ny<=m&&a[nx][ny]=='O') {
if (nx==ex&&ny==ey) return true;
nx+=fx[i];
ny+=fy[i];
}
}
return false;
}
void bfs(int x, int y) {
memset(vis,false,sizeof(vis));
memset(b,0,sizeof(b));
int h,t;
h=t=1;
b[h].x=x;
b[h].y=y;
vis[x][y]=true;
if (check(x,y)) {
cout << "0\n";
}
while(h<=t) {
for (int i=0; i<=3; i++) {
nx=b[h].x+dx[i];
ny=b[h].y+dy[i];
if (nx>=1&&nx<=n&&ny>=1&&ny<=m&&!vis[nx][ny]&&a[nx][ny]=='O') {
if (check(nx,ny)) {
cout << b[h].step+1 << endl;
return ;
}
b[++t].x=nx;
b[t].y=ny;
b[t].step=b[h].step+1;
vis[nx][ny]=true;
}
}
h++;
}
cout << "Poor Harry\n";
}
int main() {
cin >> n >> m;
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
cin >> a[i][j];
}
}
while(cin >> ex >> ey >> sx >> sy) {
if (!sx&&!sy&&!ex&&!ey)return 0;
bfs(sx,sy);
}
return 0;
}