CSES - Aalto Competitive Programming 2024 - wk2 - Mon - Results
Submission details
Task:Abandoned warehouse
Sender:Meriem
Submission time:2024-09-09 17:40:34 +0300
Language:PyPy3
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.05 sdetails
#2ACCEPTED0.05 sdetails
#3ACCEPTED0.05 sdetails
#4ACCEPTED0.05 sdetails
#5ACCEPTED0.05 sdetails
#6ACCEPTED0.13 sdetails
#70.32 sdetails
#80.27 sdetails
#90.62 sdetails
#100.67 sdetails
#110.10 sdetails
#120.08 sdetails
#130.57 sdetails
#14ACCEPTED0.05 sdetails
#15ACCEPTED0.05 sdetails
#160.76 sdetails

Code

from collections import deque
import sys

sys.setrecursionlimit(50)


def bfs(x, y, output):
    moves[x][y] = "X"
    possible_moves = [(1, 0, "D"), (0, -1, "L"), (-1, 0, "U"), (0, 1, "R")]
    end = False
    q = deque([(x, y)])
    while q:
        x, y = q.popleft()
        if graph[x][y] == "B":
            end = True
            break
        visited[x][y] = True
        for dx, dy, move in possible_moves:
            new_x, new_y = x + dx, y + dy
            if (
                0 <= new_x < n
                and 0 <= new_y < m
                and graph[new_x][new_y] != "#"
                and not visited[new_x][new_y]
            ):
                visited[new_x][new_y] = True
                moves[new_x][new_y] = move
                q.append((new_x, new_y))

    if end:
        back_path(x, y, output)
        return True
    else:
        return False


def task3(n, m, graph):
    output = []
    for i in range(n):
        for j in range(m):
            if graph[i][j] == "A":
                start = (i, j)
            elif graph[i][j] == "B":
                dest = (i, j)

    if bfs(start[0], start[1], output):
        print("YES")
        print(len(output))
        output = reversed(output)
        print("".join(output))
    else:
        print("NO")


def back_path(x, y, output):
    if moves[x][y] != "X":
        move = moves[x][y]
        output.append(move)
        if move == "U":
            back_path(x + 1, y, output)
        elif move == "D":
            back_path(x - 1, y, output)
        elif move == "L":
            back_path(x, y + 1, output)
        elif move == "R":
            back_path(x, y - 1, output)


if __name__ == "__main__":
    n, m = map(int, input().split())
    graph = [input().strip() for i in range(n)]
    visited = [[False] * m for i in range(n)]
    moves = []
    moves = [[""] * m for i in range(n)]
    task3(n, m, graph)

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:

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

correct output
YES
626
LLLDDRDDDDLDLDDLLLLLDDDDLLDLDL...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 75, in <module>
    task3(n, m, graph)
  File "input/code.py", line 46, in task3
    if bfs(start[0], start[1], output):
  File "input/code.py", line 31, in bfs
    back_path(x, y, output)
  File "input/code.py", line 62, in back_path
    back_path(x - 1, y, output)
  File "input/code.py", line 62, in back_path
    back_path(x - 1, y, output)
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  File "input/code.py", line 62, in back_path
    back_path(x - 1, y, output)
  File "input/code.py", line 62, in back_path
    back_path(x - 1, y, output)
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  File "input/code.py", line 62, in back_path
    back_path(x - 1, y, output)
  File "input/code.py", line 62, in back_path
    back_path(x - 1, y, output)
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  File "input/code.py", line 64, in back_path...

Test 8

Verdict:

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

correct output
YES
364
LULULLULLLULLLLLUULLLLUUULLLLL...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 75, in <module>
    task3(n, m, graph)
  File "input/code.py", line 46, in task3
    if bfs(start[0], start[1], output):
  File "input/code.py", line 31, in bfs
    back_path(x, y, output)
  File "input/code.py", line 60, in back_path
    back_path(x + 1, y, output)
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  File "input/code.py", line 60, in back_path
    back_path(x + 1, y, output)
  File "input/code.py", line 60, in back_path
    back_path(x + 1, y, output)
  File "input/code.py", line 60, in back_path
    back_path(x + 1, y, output)
  [Previous line repeated 2 more times]
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  File "input/code.py", line 60, in back_path
    back_path(x + 1, y, output)
  Fil...

Test 9

Verdict:

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

correct output
YES
1003
LLLLLLLLLLLLLLLLLLLLLLLLLDLLLL...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 75, in <module>
    task3(n, m, graph)
  File "input/code.py", line 46, in task3
    if bfs(start[0], start[1], output):
  File "input/code.py", line 31, in bfs
    back_path(x, y, output)
  File "input/code.py", line 62, in back_path
    back_path(x - 1, y, output)
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  File "input/code.py", line 62, in back_path
    back_path(x - 1, y, output)
  File "input/code.py", line 62, in back_path
    back_path(x - 1, y, output)
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  [Previous line repeated 3 more times]
  Fil...

Test 10

Verdict:

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

correct output
YES
947
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 75, in <module>
    task3(n, m, graph)
  File "input/code.py", line 46, in task3
    if bfs(start[0], start[1], output):
  File "input/code.py", line 31, in bfs
    back_path(x, y, output)
  File "input/code.py", line 60, in back_path
    back_path(x + 1, y, output)
  File "input/code.py", line 60, in back_path
    back_path(x + 1, y, output)
  File "input/code.py", line 60, in back_path
    back_path(x + 1, y, output)
  [Previous line repeated 67 more times]
  File "input/code.py", line 56, in back_path
    if moves[x][y] != "X":
RecursionError: maximum recursion depth exceeded

Test 11

Verdict:

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

correct output
YES
2000
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 75, in <module>
    task3(n, m, graph)
  File "input/code.py", line 46, in task3
    if bfs(start[0], start[1], output):
  File "input/code.py", line 31, in bfs
    back_path(x, y, output)
  File "input/code.py", line 60, in back_path
    back_path(x + 1, y, output)
  File "input/code.py", line 60, in back_path
    back_path(x + 1, y, output)
  File "input/code.py", line 60, in back_path
    back_path(x + 1, y, output)
  [Previous line repeated 67 more times]
  File "input/code.py", line 56, in back_path
    if moves[x][y] != "X":
RecursionError: maximum recursion depth exceeded

Test 12

Verdict:

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

correct output
YES
2000
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 75, in <module>
    task3(n, m, graph)
  File "input/code.py", line 46, in task3
    if bfs(start[0], start[1], output):
  File "input/code.py", line 31, in bfs
    back_path(x, y, output)
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  File "input/code.py", line 64, in back_path
    back_path(x, y + 1, output)
  [Previous line repeated 67 more times]
  File "input/code.py", line 56, in back_path
    if moves[x][y] != "X":
RecursionError: maximum recursion depth exceeded

Test 13

Verdict:

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

correct output
YES
499998
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 75, in <module>
    task3(n, m, graph)
  File "input/code.py", line 46, in task3
    if bfs(start[0], start[1], output):
  File "input/code.py", line 31, in bfs
    back_path(x, y, output)
  File "input/code.py", line 60, in back_path
    back_path(x + 1, y, output)
  File "input/code.py", line 60, in back_path
    back_path(x + 1, y, output)
  File "input/code.py", line 60, in back_path
    back_path(x + 1, y, output)
  [Previous line repeated 67 more times]
  File "input/code.py", line 56, in back_path
    if moves[x][y] != "X":
RecursionError: maximum recursion depth exceeded

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 75, in <module>
    task3(n, m, graph)
  File "input/code.py", line 46, in task3
    if bfs(start[0], start[1], output):
  File "input/code.py", line 31, in bfs
    back_path(x, y, output)
  File "input/code.py", line 66, in back_path
    back_path(x, y - 1, output)
  File "input/code.py", line 66, in back_path
    back_path(x, y - 1, output)
  File "input/code.py", line 66, in back_path
    back_path(x, y - 1, output)
  [Previous line repeated 67 more times]
  File "input/code.py", line 56, in back_path
    if moves[x][y] != "X":
RecursionError: maximum recursion depth exceeded