有错的地方我给你标出来了:
```
#include <bits/stdc++.h>
#define maxn 1005
using namespace std;
int d1[5] = {0, 0, 1, -1}, d2[5] = {1, -1, 0, 0};
int d3[15] = {0, 0, 1, -1, -1, -1, 1, 1}, d4[15] = {1, -1, 0, 0, -1, 1, -1, 1};
int sx, sy, ex, ey;
int n, m, fl, mp[maxn][maxn], vis[maxn][maxn];
struct node {
int x, y, s;
} p;
bool check(int x, int y) {
for (int i = 0; i < 8; i++) {
int nx = x, ny = y;
while (1) {
if (nx < 1 || nx > n || ny < 1 || ny > m)
break;
if (nx == ex && ny == ey)
return true;
if (mp[nx][ny])
break;
nx += d3[i], ny += d4[i];
}
}
return false;
}
void bfs(int x, int y) {
queue<node> q;
p.x = x, p.y = y, p.s = 0;
q.push(p);
while (q.size()) {
p = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nx = p.x + d1[i], ny = p.y + d2[i], ns = p.s + 1;
if (nx > 0 && nx <= n && ny > 0 && ny <= m && !mp[nx][ny] && !vis[nx][ny]) {
vis[nx][ny] = 1;
if (check(nx, ny)) {
cout << ns << endl;
fl = 1;
return;//这里要写return,不然会一直循环下去直到队空
}
q.push(node {nx, ny, ns});
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char x;
cin >> x;
if (x == 'X')
mp[i][j] = 1;
}
}
while (1) {
fl = 0;
memset(vis, 0, sizeof(vis));
scanf("%d %d %d %d", &ex, &ey, &sx, &sy);//哈哈,题目中是先输入奖杯坐标,再输入哈利坐标
fl = 0;
if (check(sx, sy)) {
cout << 0 << endl;
continue;
}
if (!sx && !sy && !ex && !ey) {
return 0;
}
bfs(sx, sy);
if (!fl)
puts("Poor Harry");
}
return 0;
}
```
不谢
by mons @ 2022-08-22 18:15:31