CSES - Shared codeLink to this code:
https://cses.fi/paste/473f7daa433fae6f2fd15e/
import java.util.*;
import java.io.*;
public class Main {
static long mod = 1000000007;
static long max ;
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main(String[] args) throws IOException {
FastReader sc = new FastReader();
int t = 1;
while( t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
char matrix[][] = new char[n][m];
boolean visited[][] = new boolean[n][m];
int a = 0 , b = 0 , c = 0 , d = 0;
for( int i = 0 ; i< n; i++) {
matrix[i] = sc.next().toCharArray();
for( int j = 0 ; j < m ;j++) {
if( matrix[i][j] == 'A') {
a = i;
b = j;
}
if( matrix[i][j] == 'B') {
c = i;
d = j;
}
}
}
pair root = new pair();
root.i = a;
root.j = b;
root.s = "";
bfs( root , visited , matrix , c , d, n , m);
}
out.flush();
}
private static void bfs( pair root ,boolean[][] visited, char[][] matrix, int c, int d , int n , int m) {
Queue<pair> queue = new LinkedList<>();
queue.add(root);
while( !queue.isEmpty()) {
pair top = queue.poll();
int i = top.i;
int j = top.j;
if( visited[i][j] ) {
continue;
}
visited[i][j] = true;
if( i == c && j == d) {
out.println("YES");
out.println(top.s.length());
out.println(top.s);
return;
}
if( i < n-1 && !visited[i+1][j] && matrix[i+1][j] != '#') {
pair toadd = new pair();
toadd.i = i+1;
toadd.j = j;
toadd.s = top.s + "D";
queue.add(toadd);
}
if( i > 0 && !visited[i-1][j]&& matrix[i-1][j] != '#') {
pair toadd = new pair();
toadd.i = i-1;
toadd.j = j;
toadd.s = top.s + "U";
queue.add(toadd);
}
if( j < m-1 && !visited[i][j+1]&& matrix[i][j+1] != '#') {
pair toadd = new pair();
toadd.i = i;
toadd.j = j+1;
toadd.s = top.s + "R";
queue.add(toadd);
}
if( j > 0 && !visited[i][j-1]&& matrix[i][j-1] != '#') {
pair toadd = new pair();
toadd.i = i;
toadd.j = j-1;
toadd.s = top.s + "L";
queue.add(toadd);
}
}
out.println("NO");
return;
}
public static class pair{
int i ;
int j ;
String s;
}
public static boolean ifpowof2(long n ) {
return ((n&(n-1)) == 0);
}
public static int[] nextLargerElement(int[] arr, int n) {
Stack<Integer> stack = new Stack<>();
int rtrn[] = new int[n];
rtrn[n-1] = -1;
stack.push( n-1);
for( int i = n-2 ;i >= 0 ; i--){
int temp = arr[i];
int lol = -1;
while( !stack.isEmpty() && arr[stack.peek()] <= temp){
if(arr[stack.peek()] == temp ) {
lol = stack.peek();
}
stack.pop();
}
if( stack.isEmpty()){
if( lol != -1) {
rtrn[i] = lol;
}
else {
rtrn[i] = -1;
}
}
else{
rtrn[i] = stack.peek();
}
stack.push( i);
}
return rtrn;
}
@SuppressWarnings("unused")
private static void mysort(int[] arr) {
for(int i=0;i<arr.length;i++) {
int rand = (int) (Math.random() * arr.length);
int loc = arr[rand];
arr[rand] = arr[i];
arr[i] = loc;
}
Arrays.sort(arr);
}
@SuppressWarnings("unused")
private static void mySort(long[] arr) {
for(int i=0;i<arr.length;i++) {
int rand = (int) (Math.random() * arr.length);
long loc = arr[rand];
arr[rand] = arr[i];
arr[i] = loc;
}
Arrays.sort(arr);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b)
{
return (a / gcd(a, b)) * b;
}
static long rightmostsetbit(long n) {
return n&-n;
}
static long leftmostsetbit(long n)
{
long k = (long)(Math.log(n) / Math.log(2));
return 1 << k;
}
static HashMap<Long,Long> primefactor( long n){
HashMap<Long ,Long> hm = new HashMap<>();
long temp = 0;
while( n%2 == 0) {
temp++;
n/=2;
}
if( temp!= 0) {
hm.put( 2L, temp);
}
long c = (long)Math.sqrt(n);
for( long i = 3 ; i <= c ; i+=2) {
temp = 0;
while( n% i == 0) {
temp++;
n/=i;
}
if( temp!= 0) {
hm.put( i, temp);
}
}
if( n!= 1) {
hm.put( n , 1L);
}
return hm;
}
@SuppressWarnings("unused")
private static ArrayList<Integer> allfactors(int abs) {
HashMap<Integer,Integer> hm = new HashMap<>();
ArrayList<Integer> rtrn = new ArrayList<>();
for( int i = 2 ;i*i <= abs; i++) {
if( abs% i == 0) {
hm.put( i , 0);
hm.put(abs/i, 0);
}
}
for( int x : hm.keySet()) {
rtrn.add(x);
}
if( abs != 0) {
rtrn.add(abs);
}
return rtrn;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}