CSES - Aalto Competitive Programming 2024 - wk2 - Mon - Results
Submission details
Task:Abandoned warehouse
Sender:Meriem
Submission time:2024-09-09 17:49:02 +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.13 sdetails
#7ACCEPTED0.30 sdetails
#8ACCEPTED0.26 sdetails
#9ACCEPTED0.60 sdetails
#10ACCEPTED0.65 sdetails
#11ACCEPTED0.10 sdetails
#12ACCEPTED0.08 sdetails
#13--details
#14ACCEPTED0.05 sdetails
#15ACCEPTED0.05 sdetails
#16ACCEPTED0.76 sdetails

Code

from collections import deque
import sys

sys.setrecursionlimit(1000000)


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

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

correct output
YES
626
LLLDDRDDDDLDLDDLLLLLDDDDLLDLDL...

user output
YES
626
LLLDDRDDDDDDLLDDLLDDDLDDLLLDDL...
Truncated

Test 8

Verdict: ACCEPTED

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

correct output
YES
364
LULULLULLLULLLLLUULLLLUUULLLLL...

user output
YES
364
LULULLULLLULLLLLUULLLLUUULLLLL...
Truncated

Test 9

Verdict: ACCEPTED

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

correct output
YES
1003
LLLLLLLLLLLLLLLLLLLLLLLLLDLLLL...

user output
YES
1003
DDDDDDDDDDDDDDDDDLDDDDDDDDDDDD...
Truncated

Test 10

Verdict: ACCEPTED

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

correct output
YES
947
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL...

user output
YES
947
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL...
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:

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

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

correct output
YES
1998
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
YES
1998
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...
Truncated