Advertisement
Mirbek

Labyrinth

Jan 6th, 2022
2,566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 1003;
  6.  
  7. int n, m;
  8. int vis[N][N], d[N][N];
  9. char c[N][N];
  10.  
  11. int main(){
  12.     memset(d, -1, sizeof(d));
  13.     cin >> n >> m;
  14.  
  15.     pair <int, int> Begin, End;
  16.  
  17.     for (int i = 1; i <= n; i++) {
  18.         for (int j = 1; j <= m; j++) {
  19.             cin >> c[i][j];
  20.             if (c[i][j] == 'A') {
  21.                 Begin = make_pair(i, j);
  22.             }
  23.             if (c[i][j] == 'B') {
  24.                 End = make_pair(i, j);
  25.             }
  26.         }
  27.     }
  28.  
  29.     queue < pair <int, int> > q;
  30.     vis[Begin.first][Begin.second] = 1;
  31.     d[Begin.first][Begin.second] = 0;
  32.     q.push(Begin);
  33.  
  34.     while (!q.empty()) {
  35.         pair <int, int> v = q.front();
  36.         int x = v.first, y = v.second;
  37.         q.pop();
  38.         if ((c[x - 1][y] == '.' || c[x - 1][y] == 'B') && vis[x - 1][y] == 0) {
  39.             vis[x - 1][y] = 1;
  40.             d[x - 1][y] = d[x][y] + 1;
  41.             q.push({x - 1, y});
  42.         }
  43.         if ((c[x + 1][y] == '.' || c[x + 1][y] == 'B') && vis[x + 1][y] == 0) {
  44.             vis[x + 1][y] = 1;
  45.             d[x + 1][y] = d[x][y] + 1;
  46.             q.push({x + 1, y});
  47.         }
  48.         if ((c[x][y - 1] == '.' || c[x][y - 1] == 'B') && vis[x][y - 1] == 0) {
  49.             vis[x][y - 1] = 1;
  50.             d[x][y - 1] = d[x][y] + 1;
  51.             q.push({x, y - 1});
  52.         }
  53.         if ((c[x][y + 1] == '.' || c[x][y + 1] == 'B') && vis[x][y + 1] == 0) {
  54.             vis[x][y + 1] = 1;
  55.             d[x][y + 1] = d[x][y] + 1;
  56.             q.push({x, y + 1});
  57.         }
  58.     }
  59.  
  60.     if (vis[End.first][End.second] == 0) {
  61.         cout << "NO\n";
  62.         return 0;
  63.     }
  64.  
  65.     cout << "YES\n";
  66.     cout << d[End.first][End.second] << endl;
  67.  
  68.     string path;
  69.  
  70.     while (End != Begin) {
  71.         if (d[End.first][End.second] - 1 == d[End.first - 1][End.second]) {
  72.             End.first--;
  73.             path += 'D';
  74.             continue;
  75.         }
  76.         if (d[End.first][End.second] - 1 == d[End.first + 1][End.second]) {
  77.             End.first++;
  78.             path += 'U';
  79.             continue;
  80.         }
  81.         if (d[End.first][End.second] - 1 == d[End.first][End.second - 1]) {
  82.             End.second--;
  83.             path += 'R';
  84.             continue;
  85.         }
  86.         if (d[End.first][End.second] - 1 == d[End.first][End.second + 1]) {
  87.             End.second++;
  88.             path += 'L';
  89.             continue;
  90.         }
  91.     }
  92.  
  93.     reverse(path.begin(), path.end());
  94.  
  95.     cout << path << endl;
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement