fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define nmax 1001
  5.  
  6. int n, m;
  7. char a[nmax][nmax];
  8. int monster_dist[nmax][nmax]; // Khoảng cách tối thiểu từ quái vật đến (i, j)
  9. int player_dist[nmax][nmax]; // Khoảng cách từ người chơi đến (i, j)
  10. pair<int, int> parent[nmax][nmax];
  11. char dir[nmax][nmax]; // Hướng di chuyển để đến (i, j)
  12.  
  13. int dx[4] = {1, 0, 0, -1};
  14. int dy[4] = {0, 1, -1, 0};
  15. char moves[4] = {'D', 'R', 'L', 'U'};
  16.  
  17. bool is_valid(int x, int y) {
  18. return x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] != '#';
  19. }
  20.  
  21. void bfs_monsters(vector<pair<int, int>>& monsters) {
  22. queue<pair<int, int>> q;
  23. memset(monster_dist, -1, sizeof(monster_dist));
  24. for (auto [x, y] : monsters) {
  25. q.push({x, y});
  26. monster_dist[x][y] = 0;
  27. }
  28. while (!q.empty()) {
  29. auto [x, y] = q.front(); q.pop();
  30. for (int i = 0; i < 4; i++) {
  31. int nx = x + dx[i], ny = y + dy[i];
  32. if (is_valid(nx, ny) && monster_dist[nx][ny] == -1) {
  33. monster_dist[nx][ny] = monster_dist[x][y] + 1;
  34. q.push({nx, ny});
  35. }
  36. }
  37. }
  38. }
  39.  
  40. bool bfs_player(int start_x, int start_y) {
  41. queue<pair<int, int>> q;
  42. memset(player_dist, -1, sizeof(player_dist));
  43. q.push({start_x, start_y});
  44. player_dist[start_x][start_y] = 0;
  45. while (!q.empty()) {
  46. auto [x, y] = q.front(); q.pop();
  47. // Kiểm tra nếu đã đến biên
  48. if (x == 1 || x == n || y == 1 || y == m) {
  49. return true;
  50. }
  51. for (int i = 0; i < 4; i++) {
  52. int nx = x + dx[i], ny = y + dy[i];
  53. if (is_valid(nx, ny) && player_dist[nx][ny] == -1) {
  54. // Kiểm tra ô (nx, ny) có an toàn không
  55. if (monster_dist[nx][ny] == -1 || player_dist[x][y] + 1 < monster_dist[nx][ny]) {
  56. player_dist[nx][ny] = player_dist[x][y] + 1;
  57. parent[nx][ny] = {x, y};
  58. dir[nx][ny] = moves[i];
  59. q.push({nx, ny});
  60. }
  61. }
  62. }
  63. }
  64. return false;
  65. }
  66.  
  67. void solve() {
  68. cin >> n >> m;
  69. vector<pair<int, int>> monsters;
  70. pair<int, int> start;
  71. for (int i = 1; i <= n; i++) {
  72. for (int j = 1; j <= m; j++) {
  73. cin >> a[i][j];
  74. if (a[i][j] == 'M') monsters.push_back({i, j});
  75. if (a[i][j] == 'A') start = {i, j};
  76. }
  77. }
  78. // Nếu A ở biên ngay lúc đầu
  79. if (start.first == 1 || start.first == n || start.second == 1 || start.second == m) {
  80. cout << "YES\n0\n";
  81. return;
  82. }
  83. bfs_monsters(monsters);
  84. if (bfs_player(start.first, start.second)) {
  85. cout << "YES\n";
  86. // Truy vết đường đi
  87. vector<char> path;
  88. int x, y;
  89. // Tìm điểm biên
  90. for (x = 1; x <= n; x++) {
  91. for (y = 1; y <= m; y++) {
  92. if ((x == 1 || x == n || y == 1 || y == m) && player_dist[x][y] != -1) {
  93. goto reconstruct;
  94. }
  95. }
  96. }
  97. reconstruct:
  98. while (x != start.first || y != start.second) {
  99. path.push_back(dir[x][y]);
  100. auto [px, py] = parent[x][y];
  101. x = px; y = py;
  102. }
  103. reverse(path.begin(), path.end());
  104. cout << path.size() << "\n";
  105. for (char c : path) cout << c;
  106. } else {
  107. cout << "NO";
  108. }
  109. }
  110.  
  111. int main() {
  112. solve();
  113. return 0;
  114. }
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
YES
0