Untitled
unknown
c_cpp
4 years ago
1.4 kB
9
Indexable
#include <bits/stdc++.h>
using namespace std;
struct data {
int x, y, v;
friend bool operator <(data x,data y){
return x.v<y.v;
}
};
const int N = 60;
int n,m,k;
char mp[N][N];
bool flag;
int now;
bool vis[N][N];
data t[N * N];
int total;
int ans;
bool star(int x, int y) {
return x < 1 || y < 1 || x > n || y > m || mp[x][y] == '*' || vis[x][y];
}
void dfs(int x, int y){
if(star(x, y)){
return ;
}
vis[x][y]=1;
now++;
if(x==1||y==1||x==n||y==m){
flag=0;
}
dfs(x+1,y);
dfs(x-1,y);
dfs(x,y-1);
dfs(x,y+1);
}
void fil(int x,int y){
if(x<1 || y<1 || x>n || y>m || mp[x][y] == '*'){
return ;
}
mp[x][y]='*';
fil(x+1,y);
fil(x-1,y);
fil(x,y-1);
fil(x,y+1);
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m >> k;
for(int i = 1;i <=n; i++){
cin >> mp[i] + 1;
}
for(int i = 1;i <= n; i++){
for(int j=1;j <= m; j++){
if(!vis[i][j] && mp[i][j] == '.'){
flag = 1;
now = 0;
dfs(i,j);
if(flag){
t[++total].v = now;
t[total].x = i;
t[total].y = j;
}
}
}
}
sort(t+1,t+total+1);
for(int i = 1;i <= total-k; i++){
ans += t[i].v;
fil(t[i].x,t[i].y);
}
cout << ans << '\n';
for(int i = 1;i <= n; i++){
for(int j=1;j <= m;j++){
cout << mp[i][j];
}
cout << '\n';
}
}
Editor is loading...