fork download
  1. #include <iostream>
  2. using namespace std;
  3. const int N = 1e3 + 5;
  4.  
  5. int n, m, k;
  6.  
  7. char arr[N][N];
  8. bool vis[N][N], leaves[N][N];
  9.  
  10. const int dx[] = {0, 0, 1, -1};
  11. const int dy[] = {1, -1, 0, 0};
  12.  
  13. bool valid(int x, int y)
  14. {
  15. return (x < n && x >= 0 && y < m && y >= 0 && (arr[x][y] == '.'));
  16. }
  17.  
  18. bool isLeaf(int x, int y)
  19. {
  20. for (int i = 0; i < 4; i++)
  21. {
  22. int nx = x + dx[i], ny = y + dy[i];
  23. if (valid(nx, ny))
  24. {
  25. if (!leaves[nx][ny] && !vis[nx][ny])
  26. {
  27. return 0;// not leaf
  28. }
  29. }
  30. }
  31. return 1;
  32. }
  33.  
  34. void dfs(int x, int y)
  35. {
  36. vis[x][y] = 1;
  37. for (int i = 0; i < 4; i++)
  38. {
  39. int nx = x + dx[i], ny = y + dy[i];
  40. if (valid(nx, ny) && !vis[nx][ny])
  41. {
  42. dfs(nx, ny);
  43. }
  44. }
  45. if (isLeaf(x, y))
  46. {
  47. leaves[x][y] = 1;
  48. if (k)
  49. {
  50. arr[x][y] = 'X';
  51. k--;
  52. }
  53. }
  54. }
  55.  
  56. int main()
  57. {
  58. ios_base::sync_with_stdio(0);
  59. cin.tie(NULL);
  60.  
  61. cin >> n >> m >> k;
  62. bool flag = 0;
  63. pair<int, int> index;
  64. for (int i = 0; i < n; i++)
  65. {
  66. for (int j = 0; j < m; j++)
  67. {
  68. cin >> arr[i][j];
  69. if (!flag)
  70. {
  71. if (arr[i][j] == '.')
  72. {
  73. index.first = i;
  74. index.second = j;
  75. flag = 1;
  76. }
  77. }
  78. }
  79. }
  80. cout << endl;
  81. dfs(index.first, index.second);
  82. for (int i = 0; i < n; i++)
  83. {
  84. for (int j = 0; j < m; j++)
  85. {
  86. cout << arr[i][j];
  87. }
  88. cout << endl;
  89. }
  90. return 0;
  91. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout