CSES - Shared codeLink to this code:
https://cses.fi/paste/cb1d2d877abd000927fdb8/
import java.io.*;
import java.lang.*;
import java.util.*;
// number of prime numbers less then or equal to x are --> x/ln(x)
public class b {
static FastScanner sc;
static PrintWriter pw;
static class FastScanner {
InputStreamReader is;
BufferedReader br;
StringTokenizer st;
public FastScanner() {
is = new InputStreamReader(System.in);
br = new BufferedReader(is);
}
String next() throws Exception {
while (st == null || !st.hasMoreElements())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws Exception {
return Integer.parseInt(next());
}
long nextLong() throws Exception {
return Long.parseLong(next());
}
int[] readArray(int num) throws Exception {
int arr[] = new int[num];
for (int i = 0; i < num; i++)
arr[i] = nextInt();
return arr;
}
String nextLine() throws Exception {
return br.readLine();
}
}
public static boolean power_of_two(int a) {
if ((a & (a - 1)) == 0) {
return true;
}
return false;
}
static boolean PS(double x) {
if (x >= 0) {
double i = Math.sqrt(x);
if (i % 1 != 0) {
return false;
}
return ((i * i) == x);
}
return false;
}
public static int[] ia(int n) {
int ar[] = new int[n];
return ar;
}
public static long[] la(int n) {
long ar[] = new long[n];
return ar;
}
public static void print(int ans, int t) {
System.out.println("Case" + " " + "#" + t + ":" + " " + ans);
}
static long mod = 1000000007;
static int max = Integer.MIN_VALUE;
static int min = Integer.MAX_VALUE;
// **** M A I N C O D E ****
static class pair {
int i;
int j;
String psf;
pair(int i, int j, String psf) {
this.i = i;
this.j = j;
this.psf = psf;
}
}
public static void main(String args[]) throws java.lang.Exception {
StringBuilder s = new StringBuilder();
sc = new FastScanner();
pw = new PrintWriter(System.out);
int n = sc.nextInt();
int m = sc.nextInt();
char ar[][] = new char[n][];
boolean ck[][] = new boolean[n][m];
for (int i = 0; i < n; i++) {
ar[i] = sc.next().toCharArray();
}
ArrayDeque<pair> q = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (ar[i][j] == 'A') {
q.add(new pair(i, j, ""));
break;
}
}
}
String ans = "";
while (q.size() > 0) {
pair h = q.remove();
if (h.i < 0 || h.j < 0 || h.i >= n || h.j >= m || ck[h.i][h.j] || ar[h.i][h.j] == '#') {
continue;
}
if (ar[h.i][h.j] == 'B') {
ans = h.psf;
break;
}
ck[h.i][h.j] = true;
// System.out.println(h.psf);
int i = h.i;
int j = h.j;
String psf = h.psf;
q.add(new pair(i + 1, j, psf + 'D'));
q.add(new pair(i - 1, j, psf + 'U'));
q.add(new pair(i, j - 1, psf + 'L'));
q.add(new pair(i, j + 1, psf + 'R'));
}
s.append(ans.length() == 0 ? "NO" : "YES" + "\n" + ans.length() + "\n" + ans);
pw.print(s);
pw.close();
}
}