Advertisement
Alex-Flexer

Untitled

Apr 4th, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <queue>
  4. #include <map>
  5. #include <vector>
  6. #include <set>
  7. #include <cmath>
  8. #include <iomanip>
  9. #include <algorithm>
  10. #include <fstream>
  11.  
  12. using namespace std;
  13. typedef long long ll;
  14.  
  15. struct Node
  16. {
  17. set<char> keys;
  18. int x, y;
  19. int dis;
  20.  
  21. Node(int x, int y, set<char> keys, int dis)
  22. {
  23. this->x = x;
  24. this->y = y;
  25. this->keys = keys;
  26. this->dis = dis;
  27. }
  28. };
  29.  
  30. vector<vector<pair<bool, set<char>>>> was;
  31. vector<int> dx = { 0, 1, 0, -1 };
  32. vector<int> dy = { 1, 0, -1, 0 };
  33.  
  34. bool check(int x, int y)
  35. {
  36. return((y >= 0 && x >= 0) && (y < was.size() && x < was[0].size()));
  37. }
  38.  
  39. int shortestPathAllKeys(vector<string>& grid) {
  40. queue<Node> q;
  41. was.resize(grid.size(), vector<pair<bool, set<char>>>(grid[0].size()));
  42. int amount_keys = 0;
  43. pair<int, int> spos;
  44. for (int i = 0; i < grid.size(); i++)
  45. {
  46. string& s = grid[i];
  47. for (int j = 0; j < grid[i].size(); j++)
  48. {
  49. char c = s[j];
  50. if (c >= 'a' && c <= 'z')
  51. {
  52. amount_keys++;
  53. }
  54. else if (c == '@')
  55. {
  56. spos = { i, j };
  57. }
  58. }
  59. }
  60.  
  61. //map<char, bool> m;
  62. q.push(Node(spos.second, spos.first, {}, 0));
  63. was[spos.first][spos.second] = { true, {} };
  64.  
  65. while (!q.empty())
  66. {
  67. int x = q.front().x;
  68. int y = q.front().y;
  69. set<char> keys = q.front().keys;
  70. int dis = q.front().dis;
  71.  
  72. if (x == 4 && y == 2)
  73. {
  74. cout << 'aa';
  75. }
  76.  
  77. q.pop();
  78.  
  79. for (int i = 0; i < 4; i++)
  80. {
  81. int newx = x + dx[i];
  82. int newy = y + dy[i];
  83. if (!check(newx, newy))
  84. {
  85. continue;
  86. }
  87.  
  88. if (was[newy][newx].first)
  89. {
  90. bool flag = true;
  91. for (auto k : was[newy][newx].second)
  92. {
  93. if (keys.find(k) == keys.end())
  94. {
  95. flag = false;
  96. break;
  97. }
  98. }
  99. if (flag)
  100. {
  101. continue;
  102. }
  103. }
  104.  
  105. char c = grid[newy][newx];
  106. if (c == '.')
  107. {
  108. q.push(Node(newx, newy, keys, dis + 1));
  109. }
  110. else if (c >= 'a' && c <= 'z')
  111. {
  112. keys.insert(c);
  113. if (keys.size() == amount_keys)
  114. {
  115. return dis + 1;
  116. }
  117.  
  118. q.push(Node(newx, newy, keys, dis + 1));
  119. }
  120. else if (c >= 'A' && c <= 'Z' && keys.find(c + 32) != keys.end())
  121. {
  122. q.push(Node(newx, newy, keys, dis + 1));
  123. }
  124.  
  125. was[newy][newx].first = true;
  126. was[newy][newx].second.insert(keys.begin(), keys.end());
  127. }
  128. }
  129. return -1;
  130.  
  131. }
  132.  
  133. int main()
  134. {
  135. vector<string> v = {
  136. "@...a",
  137. ".###A",
  138. "b.BCc"
  139. };
  140. cout << shortestPathAllKeys(v);
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement