D_L3

SDA - hw11 - task2

Dec 16th, 2023
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <set>
  6. #include <climits>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. bool matrix[1000][1000];
  12.  
  13. int bigger = INT_MIN;
  14. int counter = 0;
  15. int n, m;
  16.  
  17. int finalDist[1000][1000];
  18.  
  19. struct Point {
  20. int x;
  21. int y;
  22. Point(int x, int y) : x(x), y(y){}
  23. bool operator==(const Point other) const {
  24. return x == other.x && y == other.y;
  25. }
  26. bool operator<(const Point other) const {
  27. return x < other.x || (x == other.x && y < other.y);
  28. }
  29. };
  30.  
  31. struct comparator {
  32. bool operator()(pair<int, Point> lhs, pair<int, Point> rhs) const {
  33. return lhs.first < rhs.first || (lhs.first == rhs.first && lhs < rhs);
  34. }
  35. };
  36.  
  37. bool isEnd(Point& point) {
  38. return point.x == 0 || point.x >= n - 1 || point.y == 0 || point.y >= m - 1;
  39. }
  40.  
  41. vector<Point> getNeighbours(const Point& point){
  42. vector<Point> result;
  43. if (point.x != 0)
  44. result.push_back(Point(point.x - 1, point.y));
  45. if (point.x != n - 1)
  46. result.push_back(Point(point.x + 1, point.y));
  47.  
  48. if (point.y != 0)
  49. result.push_back(Point(point.x, point.y - 1));
  50. if (point.y != m - 1)
  51. result.push_back(Point(point.x, point.y + 1));
  52. return result;
  53. }
  54.  
  55. int findShortest(const Point& start, vector<vector<int>>& distances) {
  56. set<pair<int, Point>, comparator> queue; // dist, idx
  57. queue.insert({ 0, start });
  58. distances[start.x][start.y] = 0;
  59. while (!queue.empty())
  60. {
  61. auto curr = *queue.begin();
  62. queue.erase(curr);
  63.  
  64. if (isEnd(curr.second))
  65. {
  66. return curr.first;
  67. }
  68.  
  69. auto neighbours = getNeighbours(curr.second);
  70. for (auto& neighbour : neighbours)
  71. {
  72. if (finalDist[neighbour.x][neighbour.y] != INT_MAX) {
  73. queue.insert({ (curr.first + finalDist[neighbour.x][neighbour.y]), Point((neighbour.x + n), (neighbour.y + m))});
  74. continue;
  75. }
  76.  
  77. int finalTime = curr.first;
  78. if (!matrix[neighbour.x][neighbour.y]) {
  79. finalTime++;
  80. }
  81.  
  82. if (finalTime < distances[neighbour.x][neighbour.y])
  83. {
  84. auto curr = queue.find({ distances[neighbour.x][neighbour.y], neighbour });
  85. if (curr != queue.end())
  86. queue.erase(curr);
  87. queue.insert({ finalTime, neighbour });
  88. distances[neighbour.x][neighbour.y] = finalTime;
  89. }
  90. }
  91. }
  92.  
  93. return -1;
  94. }
  95.  
  96. int main() {
  97. cin >> n >> m;
  98. char ch;
  99.  
  100. for (int i = 0; i < n; i++)
  101. {
  102. for (int j = 0; j < m; j++)
  103. {
  104. cin >> ch;
  105. matrix[i][j] = (ch == '.');
  106. finalDist[i][j] = INT_MAX;
  107. }
  108. }
  109.  
  110.  
  111. for (size_t i = 0; i < n; i++)
  112. {
  113. for (size_t j = 0; j < m; j++)
  114. {
  115. if (matrix[i][j])
  116. {
  117. vector<vector<int>> distances(n, vector<int>(m, INT_MAX));
  118. int dist = findShortest(Point(i, j), distances);
  119. finalDist[i][j] = dist;
  120. if (dist > bigger) {
  121. bigger = dist;
  122. counter = 1;
  123. }
  124. else if (dist == bigger) {
  125. counter++;
  126. }
  127. }
  128. }
  129. }
  130.  
  131. cout << counter;
  132. return 0;
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment