Submission details
Task:Abandoned warehouse
Sender:aalto26bm_024
Submission time:2026-09-07 19:11:00 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#1--details
#2--details
#3--details
#4--details
#5--details
#6--details
#7--details
#8--details
#9--details
#10--details
#11--details
#12--details
#13--details
#14--details
#15--details
#16--details

Code

import sys
from collections import deque


def iix(i: int, j: int) -> int:
    return i * m + j


input = sys.stdin.readline

n, m = map(int, input().split())
inpt = []
for _ in range(n):
    inpt.append(list(input()))


start_node = (0, 0)
exit_node = (0, 0)
got1 = False
got2 = False
for i in range(n):
    for j in range(m):
        if inpt[i][j] == "A":
            start_node = (i, j)
            got1 = True
        elif inpt[i][j] == "B":
            exit_node = (i, j)
            got2 = True
        if got1 and got2:
            break


moves = [
    (1, 0),
    (0, 1),
    (-1, 0),
    (0, -1),
]

q = deque([start_node])
prev = [-1] * (n * m)


def bfs() -> bool:
    while q:
        i, j = q.popleft()
        if (i, j) == exit_node:
            return True
        for ii, jj in moves:
            if 0 <= i + ii < n and 0 <= j + jj < m:
                ix = iix(i + ii, j + jj)
                if prev[ix] == -1:
                    prev[ix] = iix(i, j)
                    q.append((i + ii, j + jj))

    return False


found = bfs()

if found:
    print("YES")
    node = iix(exit_node[0], exit_node[1])
    trace = [node]
    while node != start_node:
        node = prev[node]
        trace.append(node)
    print(len(trace) - 1)

    node = trace.pop()
    out = ""
    while len(trace) > 0:
        next = trace[-1]
        if next > node:
            if next == node + 1:
                out += "R"
            else:
                out += "D"
        else:
            if next == node - 1:
                out += "L"
            else:
                out += "U"
        node = trace.pop()
    print(out)
else:
    print("NO")

Test details

Test 1

Verdict:

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

correct output
NO

user output
(empty)

Test 2

Verdict:

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

correct output
NO

user output
(empty)

Test 3

Verdict:

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

correct output
YES
3
LLD

user output
(empty)

Test 4

Verdict:

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

correct output
YES
1
R

user output
(empty)

Test 5

Verdict:

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

correct output
YES
3
RDD

user output
(empty)

Test 6

Verdict:

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

correct output
NO

user output
(empty)

Test 7

Verdict:

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

correct output
YES
626
LLLDDRDDDDLDLDDLLLLLDDDDLLDLDL...

user output
(empty)

Test 8

Verdict:

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

correct output
YES
364
LULULLULLLULLLLLUULLLLUUULLLLL...

user output
(empty)

Test 9

Verdict:

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

correct output
YES
1003
LLLLLLLLLLLLLLLLLLLLLLLLLDLLLL...

user output
(empty)

Test 10

Verdict:

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

correct output
YES
947
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL...

user output
(empty)

Test 11

Verdict:

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

correct output
YES
2000
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 12

Verdict:

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

correct output
YES
2000
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
(empty)

Test 13

Verdict:

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

correct output
YES
499998
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 14

Verdict:

input
1 3
A.B

correct output
YES
2
RR

user output
(empty)

Test 15

Verdict:

input
2 2
##
AB

correct output
YES
1
R

user output
(empty)

Test 16

Verdict:

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

correct output
YES
1998
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
(empty)