蒟蒻求助,代码奉上:
#include<iostream>
#include<cstring>
using namespace std;
const int N = 510;
int n, m, k, cnt;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
char a[N][N];
bool vis[N][N];
void dfs(int xx, int yy) {
vis[xx][yy] = true;
for (int i = 0;i < 4; i++) {
int x = xx + dx[i];
int y = yy + dy[i];
if (0 <= x && x < n && 0 <= y && y < m && !vis[x][y] && a[x][y] != '#'){
dfs(x,y);
}
}
if (cnt){
a[xx][yy] = 'X';
cnt--;
}
}
int main() {
cin >> n >> m >> k;
for (int i = 0; i < n; i++){
cin >> a[i];
}
cnt = k;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (cnt && a[i][j] == '.') {
memset(vis,0,sizeof vis);
dfs(i,j);
}
}
}
for (int i = 0; i < n; i++){
cout << a[i] <<endl;
}
return 0;
}
救救我吧(UnknownError)