Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1007;
- string v[N];
- bool vis[N][N];
- string ans[N][N];
- queue<pair<int, int>> q;
- int n, m;
- int dx[] = {-1, 0, 1, 0};
- int dy[] = {0, -1, 0, 1};
- void bfs(int x, int y) {
- vis[x][y] = true;
- q.push({x, y});
- while(!q.empty()) {
- auto p = q.front();
- q.pop();
- int i = p.first, j = p.second;
- for(int _ = 0; _ < 4; _++) {
- 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[_]]) {
- string s = ans[i][j];
- vis[i + dx[_]][j + dy[_]] = true;
- switch(_) {
- case 0:
- s += 'U';
- ans[i + dx[_]][j + dy[_]] = s;
- q.push({i + dx[_], j + dy[_]});
- break;
- case 1:
- s += 'L';
- ans[i + dx[_]][j + dy[_]] = s;
- q.push({i + dx[_], j + dy[_]});
- break;
- case 2:
- s += 'D';
- ans[i + dx[_]][j + dy[_]] = s;
- q.push({i + dx[_], j + dy[_]});
- break;
- case 3:
- s += 'R';
- ans[i + dx[_]][j + dy[_]] = s;
- q.push({i + dx[_], j + dy[_]});
- break;
- default:
- break;
- }
- }
- }
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- freopen("in.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- cin >> n >> m;
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- }
- int xb = 0, yb = 0;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- if(v[i][j] == 'A') {
- bfs(i, j);
- }
- if(v[i][j] == 'B') {
- xb = i, yb = j;
- }
- }
- }
- if(!vis[xb][yb]) {
- cout << "NO\n";
- } else {
- cout << "YES\n";
- cout << ans[xb][yb].size() << "\n";
- cout << ans[xb][yb] << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement