Advertisement
Derga

Untitled

Jun 2nd, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <deque>
  4. #include <iomanip>
  5. #include <iostream>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. struct Point {
  11. int i;
  12. int j;
  13. int dist;
  14. };
  15.  
  16. void Print(const vector<vector<int>>& map) {
  17. for (auto row : map) {
  18. for (auto elem : row) {
  19. cout << elem << ' ';
  20. }
  21. cout << '\n';
  22. }
  23. }
  24.  
  25. int main() {
  26. ios::sync_with_stdio(0);
  27. cin.tie(0);
  28. cout.tie(0);
  29.  
  30. int side_size;
  31. cin >> side_size;
  32. const int INF = side_size * side_size + 1;
  33. const int IS_PROCESSED = INF + 1;
  34. vector<vector<int>> map(side_size, vector<int>(side_size, INF));
  35. vector<vector<int>> result_map(side_size, vector<int>(side_size, -1));
  36.  
  37. for (int i = 0; i < side_size; ++i) {
  38. for (int j = 0; j < side_size; ++j) {
  39. cin >> map[i][j];
  40. if (map[i][j] == 0) continue;
  41.  
  42. result_map[i][j] = map[i][j];
  43. }
  44. }
  45.  
  46. for (int i1 = 0; i1 < side_size; ++i1) {
  47. for (int j1 = 0; j1 < side_size; ++j1) {
  48. if (result_map[i1][j1] != -1) continue;
  49.  
  50. bool is_ceil_value_found = false;
  51. deque<Point> d;
  52. vector<vector<int>> dists(side_size, vector<int>(side_size, INF));
  53. dists[i1][j1] = 0;
  54. d.emplace_back(i1, j1, dists[i1][j1]);
  55. while (!is_ceil_value_found) {
  56. int layer_size = d.size();
  57. for (int k = 0; k < layer_size; ++k) {
  58. auto [i, j, dist] = d.back();
  59.  
  60. for (int di = -1; di <= 1; ++di) {
  61. for (int dj = -1; dj <= 1; ++dj) {
  62. if (di * di + dj * dj != 1) continue;
  63. int ni = i + di;
  64. int nj = j + dj;
  65. if (ni < 0 || map.size() <= ni || nj < 0 || map.front().size() <= nj) continue;
  66. if (dists[ni][nj] < dists[i][j] + 1) continue;
  67. dists[ni][nj] = dists[i][j] + 1;
  68. d.push_back({ ni, nj, dists[ni][nj] });
  69. }
  70. }
  71. }
  72.  
  73. int cnt = 0;
  74. int value = 0;
  75. for (int i = 0; i < layer_size; ++i) {
  76. if (map[d.front().i][d.front().j] != 0 && map[d.front().i][d.front().j] != INF) {
  77. ++cnt;
  78. value = map[d.front().i][d.front().j];
  79. }
  80. d.pop_front();
  81. }
  82. if (cnt > 1) result_map[i1][j1] = 0;
  83. if (cnt == 1) {
  84. is_ceil_value_found = true;
  85. result_map[i1][j1] = value;
  86. Print(result_map);
  87. }
  88. }
  89. }
  90. }
  91. Print(result_map);
  92.  
  93.  
  94. return 0;
  95. }
  96.  
  97. /*
  98. test1
  99. 3
  100. 0 0 0
  101. 1 0 2
  102. 0 3 0
  103.  
  104. 1 0 2
  105. 1 0 2
  106. 0 3 0
  107.  
  108. 2
  109. 0 0
  110. 0 1
  111. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement