Submission details
Task:Abandoned warehouse
Sender:aalto26bm_024
Submission time:2026-09-07 20:27:20 +0300
Language:Java
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.06 sdetails
#2ACCEPTED0.06 sdetails
#3ACCEPTED0.06 sdetails
#4ACCEPTED0.06 sdetails
#5ACCEPTED0.06 sdetails
#6ACCEPTED0.21 sdetails
#7ACCEPTED0.33 sdetails
#8ACCEPTED0.33 sdetails
#9ACCEPTED0.50 sdetails
#10ACCEPTED0.50 sdetails
#11ACCEPTED0.08 sdetails
#12ACCEPTED0.07 sdetails
#13ACCEPTED0.40 sdetails
#14ACCEPTED0.06 sdetails
#15ACCEPTED0.06 sdetails
#16ACCEPTED0.47 sdetails

Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Queue;
import java.util.Set;

class Node {
    int i;
    int j;

    public Node(int i, int j) {
        this.i = i;
        this.j = j;
    }

    public Node() {
        this.i = -1;
        this.j = -1;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Node))
            return false;

        Node n2 = (Node) obj;

        return this.i == n2.i && this.j == n2.j;
    }
}

class Move {
    int x, y;

    Move(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

public class AbandonedWarehouse {

    static int n, m;
    static List<List<Integer>> adj;
    static int[] prev;
    static Queue<Node> q = new ArrayDeque<>();
    static Node startNode = new Node();
    static Node exitNode = new Node();

    static int iix(int i, int j) {
        return i * m + j;
    }

    static int iix(Node nd) {
        return nd.i * m + nd.j;
    }

    static boolean bfs() {
        return false;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        String[] first = in.readLine().trim().split("\\s+");
        n = Integer.parseInt(first[0]);
        m = Integer.parseInt(first[1]);

        char[][] inpt = new char[n][];
        for (int i = 0; i < n; i++) {
            inpt[i] = in.readLine().toCharArray();
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (inpt[i][j] == 'A') {
                    startNode.i = i;
                    startNode.j = j;
                }
                if (inpt[i][j] == 'B') {
                    exitNode.i = i;
                    exitNode.j = j;
                }
            }
        }

        prev = new int[n * m];
        Arrays.fill(prev, -1);
        boolean found = false;
        Move[] moves = {
                new Move(1, 0),
                new Move(-1, 0),
                new Move(0, 1),
                new Move(0, -1)
        };

        q.add(startNode);
        while (!q.isEmpty()) {
            Node node = q.poll();
            if (node.equals(exitNode)) {
                found = true;
                break;
            }
            int i = node.i;
            int j = node.j;
            int prv = iix(node);

            for (Move move : moves) {
                if (i + move.x >= 0 && i + move.x < n && j + move.y >= 0 && j + move.y < m
                        && inpt[i + move.x][j + move.y] != '#') {
                    Node n = new Node(i + move.x, j + move.y);
                    int ix = iix(n);
                    if (prev[ix] == -1) {
                        prev[iix(n)] = prv;
                        q.add(n);
                    }
                }
            }

        }

        StringBuilder sb = new StringBuilder();
        if (found) {
            sb.append("YES\n");
            int node = iix(exitNode);
            List<Integer> trace = new ArrayList<>();
            trace.add(node);
            int sni = iix(startNode);
            while (node != sni) {
                node = prev[node];
                trace.add(node);
            }
            sb.append(trace.size() - 1).append('\n');

            node = trace.remove(trace.size() - 1);
            StringBuilder out = new StringBuilder();
            while (!trace.isEmpty()) {
                int next = trace.get(trace.size() - 1);
                if (next > node) {
                    if (next == node + 1) {
                        out.append('R');
                    } else {
                        out.append('D');
                    }
                } else {
                    if (next == node - 1) {
                        out.append('L');
                    } else {
                        out.append('U');
                    }
                }
                node = trace.remove(trace.size() - 1);
            }
            sb.append(out).append('\n');
        } else {
            sb.append("NO\n");
        }
        System.out.print(sb);
    }
}

Test details

Test 1

Verdict: ACCEPTED

input
10 10
##.A######
#.##.##.##
#####..###
.#########
...

correct output
NO

user output
NO

Test 2

Verdict: ACCEPTED

input
10 10
B#..##.#..
#....A##..
#.....#..#
.#......#.
...

correct output
NO

user output
NO

Test 3

Verdict: ACCEPTED

input
10 10
...#..A.#.
....B...##
...#......
..........
...

correct output
YES
3
LLD

user output
YES
3
DLL

Test 4

Verdict: ACCEPTED

input
10 10
.#........
..........
..........
........#.
...

correct output
YES
1
R

user output
YES
1
R

Test 5

Verdict: ACCEPTED

input
10 10
..........
..........
..........
..........
...

correct output
YES
3
RDD

user output
YES
3
DDR

Test 6

Verdict: ACCEPTED

input
1000 1000
##.###..######.#########.###.#...

correct output
NO

user output
NO

Test 7

Verdict: ACCEPTED

input
1000 1000
####.#.###....#.......##.##.#....

correct output
YES
626
LLLDDRDDDDLDLDDLLLLLDDDDLLDLDL...

user output
YES
626
RDDDDDLDDDDLDDDDLLLLLLDDLLLDDL...

Test 8

Verdict: ACCEPTED

input
1000 1000
....#.##......#....#......#......

correct output
YES
364
LULULLULLLULLLLLUULLLLUUULLLLL...

user output
YES
364
UUUUUULUUUUUUUUUUULLLUUUULLUUU...

Test 9

Verdict: ACCEPTED

input
1000 1000
.................#......#........

correct output
YES
1003
LLLLLLLLLLLLLLLLLLLLLLLLLDLLLL...

user output
YES
1003
DDDDDDDDDDDDDDDDDLDDDDDDDDDDDD...

Test 10

Verdict: ACCEPTED

input
1000 1000
.................................

correct output
YES
947
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL...

user output
YES
947
UUUUUUUUUUUUUUUUUUUUUUUUUUUUUU...

Test 11

Verdict: ACCEPTED

input
1000 3
A#B
.#.
.#.
.#.
...

correct output
YES
2000
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
YES
2000
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

Test 12

Verdict: ACCEPTED

input
3 1000
A................................

correct output
YES
2000
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
YES
2000
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

Test 13

Verdict: ACCEPTED

input
999 999
A#...#...#...#...#...#...#...#...

correct output
YES
499998
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
YES
499998
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

Test 14

Verdict: ACCEPTED

input
1 3
A.B

correct output
YES
2
RR

user output
YES
2
RR

Test 15

Verdict: ACCEPTED

input
2 2
##
AB

correct output
YES
1
R

user output
YES
1
R

Test 16

Verdict: ACCEPTED

input
1000 1000
A................................

correct output
YES
1998
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
YES
1998
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...