Untitled
unknown
plain_text
2 years ago
730 B
11
Indexable
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_distEditor is loading...