CSES - Aalto Competitive Programming 2024 - wk2 - Mon - Results
Submission details
Task:Abandoned warehouse
Sender:Mojojijo
Submission time:2024-09-10 21:13:11 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.05 sdetails
#2ACCEPTED0.05 sdetails
#3ACCEPTED0.05 sdetails
#4ACCEPTED0.05 sdetails
#5ACCEPTED0.05 sdetails
#6ACCEPTED0.10 sdetails
#7ACCEPTED0.51 sdetails
#8ACCEPTED0.41 sdetails
#9--details
#10--details
#11ACCEPTED0.09 sdetails
#12ACCEPTED0.07 sdetails
#13--details
#14ACCEPTED0.05 sdetails
#15ACCEPTED0.05 sdetails
#16--details

Code

from collections import deque

def bfs(grid, n, m, start, end):

    directions = {'L': (0, -1), 'R': (0, 1), 'U': (-1, 0), 'D': (1, 0)}
    reverse_dir = {v: k for k, v in directions.items()}  

    queue = deque([start])

    visited = [[False] * m for _ in range(n)]
    visited[start[0]][start[1]] = True
    
    parent = {}
    
    while queue:
        r, c = queue.popleft()
        
        if (r, c) == end:
            path = []
            while (r, c) != start:
                pr, pc = parent[(r, c)]

                direction = reverse_dir[(r - pr, c - pc)]
                path.append(direction)
                r, c = pr, pc
            return len(path), ''.join(path[::-1])  
        
        for _, (dr, dc) in directions.items():
            nr, nc = r + dr, c + dc
            if 0 <= nr < n and 0 <= nc < m and not visited[nr][nc]:
                if grid[nr][nc] == '.' or grid[nr][nc] == 'B':
                    visited[nr][nc] = True
                    parent[(nr, nc)] = (r, c)  
                    queue.append((nr, nc))

    return None


n, m = map(int, input().split())
grid = [input().strip() for _ in range(n)]

start = None
end = None
for i in range(n):
    for j in range(m):
        if grid[i][j] == 'A':
            start = (i, j)
        elif grid[i][j] == 'B':
            end = (i, j)

result = bfs(grid, n, m, start, end)

if result:
    length, path = result
    print("YES")
    print(length)
    print(path)
else:
    print("NO")

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

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: 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:

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

correct output
YES
499998
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

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:

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

correct output
YES
1998
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
(empty)