CSES - Shared codeLink to this code: https://cses.fi/paste/3105a779e810d19e2c65b6/
/********************************/
// //
// Code By- //
// //
// ******* *** //
// * * //
// * * *** //
// * ushar *** * upta //
// //
// aka Algoristy (अल्गोरिस्टी) //
// //
/********************************/
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
typedef long long int in;
typedef pair<in, in> pii;
typedef vector<in> vi;
typedef vector<vector<in>> vii;
typedef unsigned long long ull;
typedef long double lld;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define loop(i, b) for(in i=0;i<b;i++)
#define dloop(i, a, b) for(in i=a;i<=b;i++)
#define rloop(i,n) for(in i=n-1;i>=0;i--)
#define drloop(i, a, n) for(in i=n;i>=a;i--)
#define pb(a) push_back(a)
#define all(a) a.begin(),a.end()
#define trav(a,b) for(auto &a: b)
#define rtrav(a,b) for (auto a = b.rbegin(); a != b.rend(); a++) //use*a
#define mod1 1000000007
#define mod2 998244353
#define ff first
#define ss second
#define elif else if
#define gcd(a, b) (__gcd((a), (b)))
inline in lcm(in a, in b) {return a / gcd(a, b) * b;}
const in PI = acos((in) - 1);
in powerm(in x, in y, in m) {
in res = 1; x = x % m; if (x == 0) return 0;
while (y > 0) {if (y & 1) res = (res * x) % m; y = y >> 1; x = (x * x) % m;} return res;
}
in power(in x, in y) {
in res = 1; if (x == 0) return 0;
while (y > 0) {if (y & 1) res = (res * x); y = y >> 1; x = (x * x);} return res;
}
inline in inv(in a, in p = mod1) {return powerm(a, p - 2, p);}
in summ(in a, in b, in m = mod1) {return (a % m + b % m) % m;}
in difm(in a, in b, in m = mod1) {return (a % m - b % m) % m;}
in mulm(in a, in b, in m = mod1) {return (a % m * b % m) % m;}
in divm(in a, in b, in m = mod1) {return mulm(a, inv(b, m), m);}
#ifndef ONLINE_JUDGE
#define debug(bnm) cerr << #bnm <<" "; _print(bnm); cerr << endl
#else
#define debug(bnm)
#endif
void _print(in t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
#define MAX 1000001
void preSolve(){}
in ans = INT_MAX;
string result;
in xx[4] = {0, 0, 1, -1};
in yy[4] = {-1, 1, 0, 0};
string path = "LRDU";
char arr[1000][1000];
char pos[1000][1000];
in vis[1000][1000];
in n,m;
bool validity(in x, in y){
if(x < 0 || y < 0) return false;
if(x >= n || y >= m) return false;
if(vis[x][y] == 1) return false;
vis[x][y] = 1;
return true;
}
in next_of(char path){
if(path == 'R') return 0;
if(path == 'L') return 1;
if(path == 'U') return 2;
else return 3;
}
void Solve(in tx) {
cin>>n>>m;
memset(vis, -1, sizeof(vis));
pii begin, end;
loop(i, n){
loop(j, m){
cin>>arr[i][j];
if(arr[i][j] == 'A') begin = {i, j};
elif(arr[i][j] == 'B') end = {i, j};
elif(arr[i][j] == '#') vis[i][j] = 1;
}
}
vis[begin.ff][begin.ss] = 1;
debug(begin);
debug(end);
queue<pii> q;
q.push(begin);
pos[end.ff][end.ss] = '.';
while(!q.empty()){
pii block = q.front();
q.pop();
bool flag = false;
loop(i, 4){
in posx = block.ff + xx[i];
in posy = block.ss + yy[i];
if(validity(posx, posy)){
q.push({posx, posy});
pos[posx][posy] = path[i];
if(make_pair(posx, posy) == end) flag = true;
}
}
if(flag) break;
}
string result = "";
pii block = end;
if(pos[end.ff][end.ss] != '.'){
result.pb(pos[end.ff][end.ss]);
while(true){
in nxt = next_of(result.back());
block.ff += xx[nxt];
block.ss += yy[nxt];
if(block == begin) break;
result.pb(pos[block.ff][block.ss]);
}
}
if(result.size() == 0) cout<<"NO"<<endl;
else{
reverse(all(result));
cout<<"YES"<<endl;
cout<<result.size()<<endl;
cout<<result<<endl;
}
}
int main() {
fast;
#ifndef ONLINE_JUDGE
freopen("Error.tbnmt", "w", stderr);
#endif
preSolve();
in t = 1;
// cin >> t;
loop(i, t)
Solve(i);
return 0;
}