fork download
  1. /* بِسْمِ اللَّـهِ الرَّحْمَـٰنِ الرَّحِيم
  2.  
  3.   (وَاصبِر فَإِنَّ اللَّهَ لا يُضيعُ أَجرَ المُحسِنينَ)
  4. */
  5. #include<bits/stdc++.h>
  6. #define ll long long
  7. #define endl "\n"
  8. // ياعم صلي علي النبي
  9. //alt + ctrl + l
  10. using namespace std;
  11. int dx[] = {1, 0, -1, 0};
  12. int dy[] = {0, 1, 0, -1};
  13.  
  14. struct node {
  15. int vertex;
  16. int color;
  17. };
  18.  
  19. const int N = 502;
  20. char mat[N][N];
  21. bool vis[N][N];
  22. int n, m, k;
  23.  
  24. int nofdots = 0, cnt = 0;
  25. bool isin(int i, int j) { return i >= 1 && i <= n && j >= 1 && j <= m; }
  26. bool target = false;
  27.  
  28. void dfs(int i, int j) {
  29. if (cnt >= nofdots - k )return;
  30. vis[i][j] = true;
  31. int x, y;
  32.  
  33. for (int d = 0; d < 4; d++) {
  34. x = i + dx[d];
  35. y = j + dy[d];
  36. if(mat[x][y]=='.' and !vis[x][y]) {
  37. cnt++;
  38. dfs(x,y);
  39. }
  40. }
  41.  
  42. //time : O( V + E )
  43. }
  44.  
  45. void solve() {
  46. cin >> n >> m >> k;
  47. int x,y;
  48. for (int i = 1; i <= n; i++) {
  49. for (int j = 1; j <= m; j++) {
  50. cin >> mat[i][j];
  51. vis[i][j] = false;
  52. if(mat[i][j] == '.')nofdots++,x=i,y=j;
  53. }
  54. }
  55.  
  56.  
  57. dfs(1,1);
  58. for (int i = 1; i <= n; i++) {
  59. for (int j = 1; j <= m; j++) {
  60. if(mat[i][j]=='#')cout<<'#';
  61. else if(vis[i][j])cout<<'.';
  62. else cout<<'X';
  63. }
  64. cout<<endl;
  65. }
  66. }
  67.  
  68. int main() {
  69. ios_base::sync_with_stdio(0);
  70. cin.tie(0);
  71. cout.tie(0);
  72. int t = 1;
  73.  
  74. // cin>>t;
  75. while (t--) {
  76. solve();
  77. }
  78. return 0;
  79. }
  80.  
  81. /*
  82.   (وَأَنْ لَيْسَ لِلإِنسَانِ إِلَّا مَا سَعَى وَأَنَّ سَعْيَهُ سَوْفَ يُرَى)
  83. */
  84.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty