Advertisement
cosenza987

Untitled

Oct 16th, 2021
1,278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 1007;
  6. string v[N];
  7. bool vis[N][N];
  8. string ans[N][N];
  9. queue<pair<int, int>> q;
  10. int n, m;
  11. int dx[] = {-1, 0, 1, 0};
  12. int dy[] = {0, -1, 0, 1};
  13.  
  14. void bfs(int x, int y) {
  15.     vis[x][y] = true;
  16.     q.push({x, y});
  17.     while(!q.empty()) {
  18.         auto p = q.front();
  19.         q.pop();
  20.         int i = p.first, j = p.second;
  21.         for(int _ = 0; _ < 4; _++) {
  22.             if(i + dx[_] >= 0 and i + dx[_] < n and j + dy[_] >= 0 and j + dy[_] < m and (v[i + dx[_]][j + dy[_]] == '.' or v[i + dx[_]][j + dy[_]] == 'B') and !vis[i + dx[_]][j + dy[_]]) {
  23.                 string s = ans[i][j];
  24.                 vis[i + dx[_]][j + dy[_]] = true;
  25.                 switch(_) {
  26.                     case 0:
  27.                         s += 'U';
  28.                         ans[i + dx[_]][j + dy[_]] = s;
  29.                         q.push({i + dx[_], j + dy[_]});
  30.                         break;
  31.                     case 1:
  32.                         s += 'L';
  33.                         ans[i + dx[_]][j + dy[_]] = s;
  34.                         q.push({i + dx[_], j + dy[_]});
  35.                         break;
  36.                     case 2:
  37.                         s += 'D';
  38.                         ans[i + dx[_]][j + dy[_]] = s;
  39.                         q.push({i + dx[_], j + dy[_]});
  40.                         break;
  41.                     case 3:
  42.                         s += 'R';
  43.                         ans[i + dx[_]][j + dy[_]] = s;
  44.                         q.push({i + dx[_], j + dy[_]});
  45.                         break;
  46.                     default:
  47.                         break;
  48.                 }
  49.             }
  50.         }
  51.     }
  52. }
  53.  
  54. int main() {
  55.     ios_base::sync_with_stdio(false);
  56.     cin.tie(nullptr);
  57.     freopen("in.txt", "r", stdin);
  58.     freopen("output.txt", "w", stdout);
  59.     cin >> n >> m;
  60.     for(int i = 0; i < n; i++) {
  61.         cin >> v[i];
  62.     }
  63.     int xb = 0, yb = 0;
  64.     for(int i = 0; i < n; i++) {
  65.         for(int j = 0; j < m; j++) {
  66.             if(v[i][j] == 'A') {
  67.                 bfs(i, j);
  68.             }
  69.             if(v[i][j] == 'B') {
  70.                 xb = i, yb = j;
  71.             }
  72.         }
  73.     }
  74.     if(!vis[xb][yb])  {
  75.         cout << "NO\n";
  76.     } else {
  77.         cout << "YES\n";
  78.         cout << ans[xb][yb].size() << "\n";
  79.         cout << ans[xb][yb] << "\n";
  80.     }
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement