A:
从一个空点开始搜,建立搜索树。然后从深度最深的节点开始删除,这样就能保证深度低的节点依然连通。
code:
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <set>
#include <map>
using namespace std;
#define N 512
#define ALL(x) x.begin(),x.end()
#define CLR(x,a) memset(x,a,sizeof(x))
typedef long long ll;
typedef pair<int,int> PI;
const int INF = 0x3fffffff;
const int MOD = 1000000007;
const double EPS = 1e-7;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
char mp[N][N];
int n,m,k;
vector<PI> h[N*N];
bool vis[N][N];
void dfs(int x,int y,int step)
{
vis[x][y]=true;
h[step].push_back((PI){x,y});
for(int i=0;i<4;i++){
int xx=dx[i]+x;
int yy=dy[i]+y;
if(xx<0 || xx>=n || yy<0 || yy>=m) continue;
if(vis[xx][yy] || mp[xx][yy]=='#') continue;
dfs(xx,yy,step+1);
}
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<n;i++) scanf("%s",mp[i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++) if(mp[i][j]=='.'){
dfs(i,j,0);
break;
}
for(int i=n*m+1;i>=0;i--){
if(h[i].size()){
int Min=min(k,(int)h[i].size());
for(int j=0;j<Min;j++)
mp[h[i][j].first][h[i][j].second]='X';
k-=Min;
if(k==0)
break;
}
}
for(int i=0;i<n;i++)
printf("%s\n",mp[i]);
return 0;
}