Files
python/TangDou/Bfs/LQBS_13_ZJ_STEMA_5.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

56 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
/**
3 4
P P P P
P O P X
P O X P
O P P P
答案10
*/
using namespace std;
const int N = 110;
char a[N][N];
struct Node {
int x;
int y;
int step;
};
queue<Node> q;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int cnt;
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
//记录初始病人
if (a[i][j] == 'X') q.push({i, j, 0});
}
//n分钟
while (!q.empty()) {
auto t = q.front();
q.pop();
//执行n分钟
if (t.step == n) break;
//四个方向
for (int i = 0; i <= 3; i++) {
int tx = t.x + dx[i];
int ty = t.y + dy[i];
if (tx >= 1 && tx <= m && ty >= 1
&& ty <= m && a[tx][ty] == 'P') {
a[tx][ty] = 'X';
q.push({tx, ty, t.step + 1});
}
}
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == 'X')cnt++;
printf("%d ", cnt);
return 0;
}