CSES - Shared codeLink to this code:
https://cses.fi/paste/693ab21822bb94941292ae/
/*
Three things which are very important --
Violence - Speed - Momentum
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define setbits(x) __builtin_popcountll(x)
#define leadzero(x) __builtin_clz(x)
#define trailzero(x) __builtin_ctz(x)
#define bitsparity(x) __builtin_parity(x)
#define mod 1000000007
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define pre(y,x) fixed<<setprecision(y)<<x
#define dbg1(x) cerr << "|" << #x << ": " << (x) << "|\n";
#define dbg2(x,y) cerr << "|" << #x << ": " << (x) << "|" << #y << ": " << (y) << "|\n";
#define dbg3(x,y,z) cerr << "|" << #x << ": " << (x) << "|" << #y << ": " << (y) << "|" << #z << ": " << (z) << "|\n";
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define popb pop_back
#define endl '\n'
#define all(v) v.begin(), v.end()
#define sz(x) ((int)((x).size()))
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
ll power(ll a, ll b) {
if(b<0) return 1;
ll res=1;
while(b) {
if(b&1)
res = (res*a)%mod;
a = (a*a)%mod;
b >>= 1;
}
return res;
}
bool ispoweroftwo(ll x){ return (x&&!(x&(x-1)));}
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
const int inf = 1e9;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
char PH[] = {'U', 'R', 'D', 'L'};
//integer to char always remember (char+'0')
const double pi = acos(-1);
const ll INF = 1e18;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const int mxN = 2e3+9;
//vector<ll> adj[mxN];
int vis[mxN][mxN];
vector<string> v(mxN);
int n, m;
vector<vector<string>> path(mxN, vector<string>(mxN));
pair<ll, ll> a, b;
void bfs(int i, int j) {
vis[i][j] = 1;
queue<pair<ll, ll>> q;
q.push({i ,j});
while(!q.empty()) {
pair<ll, ll> p = q.front();
q.pop();
vis[p.ff][p.ss] = 1;
for(int i=0; i<4; ++i) {
int x = p.ff + dx[i];
int y = p.ss + dy[i];
if(x<0 || x>=n || y<0 || y>=m || v[x][y] == '#') continue;
if(!vis[x][y]) {
vis[x][y] = 1;
q.push({x, y});
path[x][y] = path[p.ff][p.ss]+PH[i];
}
// if(b.ff == x && b.ss == y) return;
}
}
}
int main() {
FIO;
cin >> n >> m;
for(int i=0; i<n; ++i) {
cin >> v[i];
}
for(int i=0; i<n; ++i) {
for(int j=0; j<m; ++j) {
if(v[i][j] == 'A') a={i, j};
else if(v[i][j] == 'B') b={i, j};
}
}
bfs(a.ff, a.ss);
if(vis[b.ff][b.ss]) {
string rev = path[b.ff][b.ss];
cout << "YES\n" << sz(rev) << "\n" << rev;
} else {
cout << "NO";
}
return 0;
}
/*
3 3
A..
###
#.B
*/