Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
730 B
1
Indexable
Never
def maze(L):
    n = len(L)
    if L[0][0] == '#' or L[n - 1][n - 1] == '#':
        return -1

    max_dist = -1
    visited = [[False for _ in range(n)] for _ in range(n)]
    stack = [(0, 0, 0)]

    while stack:
        x, y, curr_dist = stack.pop()

        if x == n - 1 and y == n - 1:
            max_dist = max(max_dist, curr_dist)
            continue

        if visited[y][x] or L[y][x] == '#':
            continue

        visited[y][x] = True

        if x + 1 < n:
            stack.append((x + 1, y, curr_dist + 1))
        if y + 1 < n:
            stack.append((x, y + 1, curr_dist + 1))
        if y - 1 > -1:
            stack.append((x, y - 1, curr_dist + 1))

    return max_dist