嗯我的想法是把奖杯的八个方向的点全部打上标记,然后哈利走到标记的点就是能看到奖杯, 记录
代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int n, m;
char a[1000][1000];
int dis[1000][1000];
int X, Y, tx, ty;
struct coss {
int x, y;
};
int dx[] = {0, 0, 0, 1, -1};
int dy[] = {0, 1, -1, 0, 0};
void bfs() {
// cout << tx << ' ' << ty << endl;
int MAX = 2139062143;
queue <coss> q;
q.push({tx, ty});
dis[tx][ty] = 0;
while(q.size()) {
// cout << "asd";
coss N = q.front();
q.pop();
if(a[N.x][N.y] == '*') {
MAX = min(MAX, dis[N.x][N.y]);
continue;
}
for(int i = 1; i <= 4; i ++) {
int nx = N.x + dx[i];
int ny = N.y + dy[i];
if(nx && ny && nx <= n && ny <= m && a[nx][ny] != 'X' && (dis[nx][ny] == 2139062143 ? true : (dis[nx][ny] > dis[N.x][N.y] + 1 ? true : false))) {
q.push({nx, ny});
dis[nx][ny] = dis[N.x][N.y] + 1;
}
}
}
if(MAX == 2139062143) {
cout << "Poor Harry";
}
else cout << MAX << endl;
}
void add(int x, int y) {
for(int i = y; a[x][i] != 'X' && i >= 1; i --) {
a[x][i] = '*';
}
for(int i = y; a[x][i] != 'X' && i <= m; i ++) {
a[x][i] = '*';
}
for(int i = x; a[i][y] != 'X' && i >= 1; i --) {
a[i][y] = '*';
}
for(int i = x; a[i][y] != 'X' && i <= n; i ++) {
a[i][y] = '*';
}
for(int i = x, j = y; a[i][j] != 'X' && i >= 1 && j >= 1; i --, j --) {
a[i][j] = '*';
}
for(int i = x, j = y; a[i][j] != 'X' && i >= 1 && j <= m; i --, j ++) {
a[i][j] = '*';
}
for(int i = x, j = y; a[i][j] != 'X' && i <= n && j <= m; i ++, j ++) {
a[i][j] = '*';
}
for(int i = x, j = y; a[i][j] != 'X' && i <= n && j >= 1; i ++, j --) {
a[i][j] = '*';
}
memset(dis, 0x7f, sizeof(dis)); //0x7f == 2139062143
bfs();
for(int i = y; a[x][i] != 'X' && i >= 1; i --) {
a[x][i] = 'O';
}
for(int i = y; a[x][i] != 'X' && i <= m; i ++) {
a[x][i] = 'O';
}
for(int i = x; a[i][y] != 'X' && i >= 1; i --) {
a[i][y] = 'O';
}
for(int i = x; a[i][y] != 'X' && i <= n; i ++) {
a[i][y] = 'O';
}
for(int i = x, j = y; a[i][j] != 'X' && i >= 1 && j >= 1; i --, j --) {
a[i][j] = 'O';
}
for(int i = x, j = y; a[i][j] != 'X' && i >= 1 && j <= m; i --, j ++) {
a[i][j] = 'O';
}
for(int i = x, j = y; a[i][j] != 'X' && i <= n && j <= m; i ++, j ++) {
a[i][j] = 'O';
}
for(int i = x, j = y; a[i][j] != 'X' && i <= n && j >= 1; i ++, j --) {
a[i][j] = 'O';
}
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++) {
cin >> a[i][j];
}
}
while(true) {
scanf("%d%d%d%d", &X, &Y, &tx, &ty);
if(!X && !Y && !tx && !ty) {
break;
}
add(X, Y);
}
return 0;
}
谢谢