You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
945 B

#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;
}