-
Notifications
You must be signed in to change notification settings - Fork 1
/
shortest-path-in-binary-matrix.py
34 lines (29 loc) · 1.12 KB
/
shortest-path-in-binary-matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Leetcode 1091. Shortest Path in Binary Matrix
#
# Link: https://leetcode.com/problems/shortest-path-in-binary-matrix/
# Difficulty: Medium
# Solution using BFS changing grid values instead of visited set.
# Complexity:
# O(M*N) time | where M and N represent the rows and cols of the input matrix
# O(1) space
class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
EMPTY, BLOCKED, VISITED = range(3)
ROWS, COLS = len(grid), len(grid[0])
DIRECTIONS = ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1))
q = deque()
if grid[0][0] == 0:
q.append((0,0,1))
grid[0][0] = VISITED
while q:
r, c, distance = q.popleft()
if ((r,c) == (ROWS - 1, COLS - 1)):
return distance
for dr, dc in DIRECTIONS:
nr, nc = r + dr, c + dc
if (nr in range(ROWS) and
nc in range(COLS) and
grid[nr][nc] == EMPTY):
q.append((nr,nc,distance+1))
grid[nr][nc] = VISITED
return -1