CSES - Shared codeLink to this code:
https://cses.fi/paste/ca53210d467e6f2f2d250f/
#include <bits/stdc++.h>
using namespace std;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define nline "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
// typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
void _print(ll 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 << "]";}
/*===================================MAIN CODE=============================================*/
int vis[1005][1005];
int n,m;
char arr[1005][1005];
vector <pair<int,string>> v[1005];
bool possible(int x,int y){
if(x<0 || x>n-1 || y<0 || y>m-1) return false;
if(vis[x][y] || arr[x][y]=='#') return false;
return true;
}
void bfs(int x,int y){
queue <pair<int,int>> q;
v[x][y].ff = 0;
vis[x][y] = 1;
q.push({x,y});
while(!q.empty()){
pair <int,int> p = q.front();
string s = v[p.ff][p.ss].ss;
int cnt = v[p.ff][p.ss].ff;
vis[p.ff][p.ss] = 1;
q.pop();
if(possible(p.ff+1,p.ss)){
q.push({p.ff+1,p.ss});
vis[p.ff+1][p.ss] = 1;
if(v[p.ff+1][p.ss].ff>cnt+1){
v[p.ff+1][p.ss] = {cnt+1,s+"D"};
}
}
if(possible(p.ff-1,p.ss)){
q.push({p.ff-1,p.ss});
vis[p.ff-1][p.ss] = 1;
if(v[p.ff-1][p.ss].ff>cnt+1){
v[p.ff-1][p.ss] = {cnt+1,s+"U"};
}
}
if(possible(p.ff,p.ss+1)){
q.push({p.ff,p.ss+1});
vis[p.ff][p.ss+1] = 1;
if(v[p.ff][p.ss+1].ff>cnt+1){
v[p.ff][p.ss+1] = {cnt+1,s+"R"};
}
}
if(possible(p.ff,p.ss-1)){
q.push({p.ff,p.ss-1});
vis[p.ff][p.ss-1] = 1;
if(v[p.ff][p.ss-1].ff>cnt+1){
v[p.ff][p.ss-1] = {cnt+1,s+"L"};
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("Input.txt", "r", stdin);
freopen("Output.txt", "w", stdout);
freopen("Error.txt", "w", stderr);
#endif
fastio();
cin >> n >> m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin >> arr[i][j];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
v[i].pb({INT_MAX,""});
}
}
pair <int,int> a,b;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(arr[i][j]=='A'){
a = {i,j};
} else if(arr[i][j]=='B'){
b = {i,j};
}
}
}
debug(a)
debug(b)
bfs(a.ff,a.ss);
if(!vis[b.ff][b.ss]){
cout << "NO";
} else {
cout << "YES\n";
cout << v[b.ff][b.ss].ff << "\n";
cout << v[b.ff][b.ss].ss;
}
return 0;
}