Files
python/TangDou/LanQiaoBei/LanQiaoBei13STEMA_ZJ_2/TD.cpp

39 lines
945 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
char a[N][N];
struct Node {
int x;
int y;
int step;
};
queue<Node> q;
int dx[] = {-1, 0, 1, 0}; // 上右下左
int dy[] = {0, 1, 0, -1}; // 上右下左
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}),cnt++;
}
while (q.size()) {
auto u = q.front();
q.pop();
if (u.step == n) break;
for (int i = 0; i <= 3; i++) {
int tx = u.x + dx[i];
int ty = u.y + dy[i];
if (tx >= 1 && a[tx][ty] == 'P' && tx <= m && ty >= 1 && ty <= m) {
a[tx][ty] = 'X';
cnt++;
q.push({tx, ty, u.step + 1});
}
}
}
printf("%d ",cnt);
return 0;
}