这这这有什么问题?
原题链接:48698.午枫爱迷宫2025-06-16 21:03:18
发布于:浙江
#include<bits/stdc++.h>
using namespace std;
int n, m;
int sx, sy, fx, fy, jx, jy, ysx, ysy;
bool vis[1010][1010];
char mp[1010][1010];
int dirx[] = {-1, 0, 1, 0};
int diry[] = {0, 1, 0, -1};
bool dfs(int x, int y, int tx, int ty, bool has_key) {
if (x == tx && y == ty) return true;
vis[x][y] = true;
for (int i = 0; i < 4; i++) {
int nx = x + dirx[i];
int ny = y + diry[i];
if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
if (mp[nx][ny] == '#') continue;
if (mp[nx][ny] == '$' && !has_key) continue;
if (!vis[nx][ny]) {
if (dfs(nx, ny, tx, ty, has_key || (mp[nx][ny] == '!'))) {
return true;
}
}
}
return false;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> mp[i][j];
if (mp[i][j] == '@') {
sx = i; sy = j;
}
if (mp[i][j] == 'N') {
fx = i; fy = j;
}
if (mp[i][j] == 'M') {
jx = i; jy = j;
}
if (mp[i][j] == '!') {
ysx = i; ysy = j;
}
}
}
memset(vis, 0, sizeof(vis));
bool can_reach_key = dfs(fx, fy, ysx, ysy, false);
memset(vis, 0, sizeof(vis));
bool can_reach_m = dfs(fx, fy, jx, jy, false);
memset(vis, 0, sizeof(vis));
bool can_reach_exit = dfs(fx, fy, sx, sy, false);
if (can_reach_key && can_reach_m) {
memset(vis, 0, sizeof(vis));
bool can_reach_exit_with_m = dfs(jx, jy, sx, sy, true);
if (can_reach_exit_with_m) {
cout << "we were here together" << endl;
} else {
cout << "NO" << endl;
}
} else if (can_reach_exit) {
cout << "sorry" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个