CSES - Aalto Competitive Programming 2024 - wk2 - Mon - Results
Submission details
Task:Abandoned warehouse
Sender:esya_rae
Submission time:2024-09-09 17:37:59 +0300
Language:Python3 (PyPy3)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.05 sdetails
#2ACCEPTED0.05 sdetails
#3ACCEPTED0.05 sdetails
#4ACCEPTED0.05 sdetails
#5ACCEPTED0.05 sdetails
#6ACCEPTED0.47 sdetails
#7ACCEPTED0.89 sdetails
#8ACCEPTED0.91 sdetails
#9ACCEPTED0.95 sdetails
#10ACCEPTED0.98 sdetails
#11ACCEPTED0.10 sdetails
#12ACCEPTED0.09 sdetails
#13ACCEPTED0.85 sdetails
#14ACCEPTED0.05 sdetails
#15ACCEPTED0.05 sdetails
#16ACCEPTED0.93 sdetails

Code

from collections import deque
n, m = map(int, input().split())
g = [[] for _ in range(n * m)]
used = [False for _ in range(n * m)]

start, finish = -1, -1

s_prev = ''
s = ''

for i in range(n):
    s_prev = s
    s = input()
    for j in range(m):
        if s[j] == "#":
            continue
        if i != 0 and s_prev[j] != '#':
            g[i * m + j].append((i - 1) * m + j)
            g[(i - 1) * m + j].append(i * m + j)
        if j != 0 and s[j - 1] != '#':
            g[i * m + j].append(i * m + j - 1)
        if j != m - 1 and s[j + 1] != '#':
            g[i * m + j].append(i * m + j + 1)

        if s[j] == 'A':
            start = i * m + j
        elif s[j] == 'B':
            finish = i * m + j

dist = [-1] * (n * m)
parent = [-1] * (n * m)

def bfs(v):
    global g, dist
    queue = deque([v])
    while queue:
        node = queue.popleft()
        for w in g[node]:
            if dist[w] == -1:
                parent[w] = node
                dist[w] = dist[node] + 1
                if w == finish:
                    break
                queue.append(w)



dist[start] = 0
bfs(start)

if dist[finish] == -1:
    print("NO")
else:
    print("YES")
    print(dist[finish])
    path = []
    path.append(finish)
    v = finish
    while parent[v] != start:
        path.append(parent[v])
        v = parent[v]
    path.append(start)
    path.reverse()
    for i in range(1, len(path)):
        if path[i] == path[i - 1] + 1:
            print('R', end='')
        elif path[i] == path[i - 1] - 1:
            print('L', end='')
        elif path[i] < path[i - 1]:
            print('U', end='')
        else:
            print('D', end='')

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
LLD

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
RDD

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
LLLDDRDDDDLDLDDLLLLLDDDDLLDLDL...
Truncated

Test 8

Verdict: ACCEPTED

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

correct output
YES
364
LULULLULLLULLLLLUULLLLUUULLLLL...

user output
YES
364
UUUUUULUUUUUUUUUUULLLUUUULLUUU...
Truncated

Test 9

Verdict: ACCEPTED

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

correct output
YES
1003
LLLLLLLLLLLLLLLLLLLLLLLLLDLLLL...

user output
YES
1003
LLLLLLLLLLLLLLLLLLLLLLLLLDLLLL...
Truncated

Test 10

Verdict: ACCEPTED

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

correct output
YES
947
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL...

user output
YES
947
UUUUUUUUUUUUUUUUUUUUUUUUUUUUUU...
Truncated

Test 11

Verdict: ACCEPTED

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

correct output
YES
2000
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
YES
2000
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...
Truncated

Test 12

Verdict: ACCEPTED

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

correct output
YES
2000
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
YES
2000
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...
Truncated

Test 13

Verdict: ACCEPTED

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

correct output
YES
499998
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
YES
499998
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...
Truncated

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
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...
Truncated