Advertisement
Alex-Flexer

Untitled

Nov 30th, 2023
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <string>
  4. #include <queue>
  5. #include <map>
  6. #include <vector>
  7. #include <set>
  8. #include <cmath>
  9. #include <iomanip>
  10. #include <algorithm>
  11. #include <stack>
  12.  
  13. using namespace std;
  14. typedef long long ll;
  15. typedef double db;
  16.  
  17. vector<int> dx = { 0, 1, 0, -1 };
  18. vector<int> dy = { 1, 0, -1, 0 };
  19. vector<char> dicrets {'D', 'R', 'U', 'L'};
  20.  
  21. struct Node
  22. {
  23. int type;
  24. bool was;
  25. bool moved;
  26. };
  27.  
  28. int n, m;
  29. vector<vector<Node>> g;
  30. vector<pair<int, int>> monsters;
  31.  
  32. bool check_is_exit(int x, int y)
  33. {
  34. return (x == 0 || y == 0 || x == m - 1 || y == n - 1);
  35. }
  36.  
  37. bool check_inside(int x, int y)
  38. {
  39. return (x >= 0 && y >= 0 && x < m && y < n);
  40. }
  41.  
  42. void move_monster(int direct, bool moving_new_pos)
  43. {
  44. for (int i = 0; i < monsters.size(); i++)
  45. {
  46. if (g[monsters[i].second][monsters[i].first].moved == false && moving_new_pos == false)
  47. {
  48. continue;
  49. }
  50. int new_y = monsters[i].second + dy[direct];
  51. int new_x = monsters[i].first + dx[direct];
  52. if (!check_inside(new_x, new_y))
  53. {
  54. continue;
  55. }
  56. if (g[new_y][new_x].type == 0)
  57. {
  58. if (g[new_y][new_x].moved)
  59. {
  60. continue;
  61. }
  62. }
  63. else if (g[new_y][new_x].type == 1)
  64. {
  65. continue;
  66. }
  67. g[new_y][new_x].moved = moving_new_pos;
  68. g[monsters[i].second][monsters[i].first].type = 2;
  69.  
  70. monsters[i].first = new_x;
  71. monsters[i].second = new_y;
  72.  
  73. g[monsters[i].second][monsters[i].first].type = 0;
  74. }
  75. }
  76.  
  77. vector<int> res;
  78.  
  79. bool dfs(int x, int y)
  80. {
  81. if (g[y][x].was)
  82. {
  83. return false;
  84. }
  85. g[y][x].was = true;
  86. if (check_is_exit(x, y))
  87. {
  88. return true;
  89. }
  90. for (int i = 0; i < 4; i++)
  91. {
  92. //move all monsters
  93. move_monster(i, true);
  94. if (g[y + dy[i]][x + dx[i]].type == 2)
  95. {
  96. if (dfs(x + dx[i], y + dy[i]))
  97. {
  98. res.push_back(i);
  99. return true;
  100. }
  101. }
  102. // move back
  103. move_monster((i + 2) % 4, false);
  104. }
  105. return false;
  106. }
  107.  
  108. int main()
  109. {
  110. cin >> n >> m;
  111. g.resize(n, vector<Node> (m));
  112. int start_x = 0;
  113. int start_y = 0;
  114. for (int i = 0; i < n; i++)
  115. {
  116. for (int j = 0; j < m; j++)
  117. {
  118. char c;
  119. cin >> c;
  120. if (c == 'A')
  121. {
  122. start_y = i;
  123. start_x = j;
  124. g[i][j].type = 2;
  125. }
  126. else if (c == 'M')
  127. {
  128. g[i][j].type = 0;
  129. monsters.push_back(make_pair(j, i));
  130. }
  131. else
  132. {
  133. g[i][j].type = ((c == '#') ? 1 : 2);
  134. }
  135. }
  136. }
  137. if (dfs(start_x, start_y))
  138. {
  139. cout << "YES" << endl << res.size() << endl;
  140. for (int i = res.size() - 1; i >= 0; i--)
  141. {
  142. cout << dicrets[res[i]];
  143. }
  144. }
  145. else
  146. {
  147. cout << "NO";
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement