fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define maxN 52
  4. char a[maxN][maxN];
  5. int visited[maxN][maxN];
  6. int mark[maxN][maxN];
  7. bool lake;
  8. int numadj,n,m;
  9. vector < pair <int, pair <int,int> > > v;
  10. void checklake(int x,int y)
  11. {
  12. visited[x][y]=1;
  13. numadj++;
  14. if(x==0||x==n-1||y==m-1||y==0)
  15. {
  16. lake=false;
  17. return ;
  18. }
  19. if(!visited[x][y+1]&&a[x][y+1]=='.')checklake(x,y+1);
  20. if(!visited[x][y-1]&&a[x][y-1]=='.')checklake(x,y-1);
  21. if(!visited[x+1][y]&&a[x+1][y]=='.')checklake(x+1,y);
  22. if(!visited[x-1][y]&&a[x-1][y]=='.')checklake(x-1,y);
  23. }
  24.  
  25. void ffill(int x,int y)
  26. {
  27. a[x][y]='*';
  28. mark[x][y]=1;
  29. if(a[x][y+1]=='.'&&!mark[x][y+1])ffill(x,y+1);
  30. if(a[x][y-1]=='.'&&!mark[x][y-1])ffill(x,y-1);
  31. if(a[x+1][y]=='.'&&!mark[x+1][y])ffill(x+1,y);
  32. if(a[x-1][y]=='.'&&!mark[x-1][y])ffill(x-1,y);
  33. }
  34. int main() {
  35. std::ios::sync_with_stdio(false);
  36. int k,ans=0;
  37. cin>>n>>m>>k;
  38. for(int i=0;i<n;i++)
  39. {
  40. for(int j=0;j<m;j++)
  41. {
  42. cin>>a[i][j];
  43.  
  44. }
  45. }
  46. for(int i=0;i<n;i++)
  47. {
  48. for(int j=0;j<m;j++)
  49. {
  50.  
  51. visited[i][j]=0;
  52. mark[i][j]=0;
  53. }
  54. }
  55.  
  56. for(int i=0;i<n;i++)
  57. {
  58. for(int j=0;j<m;j++)
  59. { numadj=0;
  60. if(a[i][j]=='.'&&!visited[i][j])
  61. { lake=true;
  62. checklake(i,j);
  63.  
  64. if(lake)
  65. {
  66. v.push_back({numadj,{i,j}});
  67. }
  68. }
  69. }
  70. }
  71. sort(v.begin(),v.end());
  72.  
  73. for(int i=0;i<v.size()-k;i++)
  74. {
  75. ans+=v[i].first;
  76. ffill(v[i].second.first,v[i].second.second);
  77. }
  78. cout<<ans<<endl;
  79. for(int i=0;i<n;i++)
  80. {
  81. for(int j=0;j<m;j++)
  82. {
  83. cout<<a[i][j];
  84. }
  85. cout<<endl;
  86. }
  87. return 0;
  88. }
  89.  
Success #stdin #stdout 0s 3492KB
stdin
5 4 1
****
*..*
****
**.*
..**
stdout
1
****
*..*
****
****
..**