-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path0909-snakes-and-ladders.py
38 lines (34 loc) · 1.18 KB
/
0909-snakes-and-ladders.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
35
36
37
38
# time complexity: O(n^2)
# space complexity: O(n^2)
from collections import deque
from typing import List
class Solution:
def snakesAndLadders(self, board: List[List[int]]) -> int:
n = len(board)
cells = [None] * (n**2 + 1)
label = 1
columns = list(range(0, n))
for row in range(n - 1, -1, -1):
for column in columns:
cells[label] = (row, column)
label += 1
columns.reverse()
dist = [-1] * (n * n + 1)
q = deque([1])
dist[1] = 0
while q:
curr = q.popleft()
for next in range(curr + 1, min(curr + 6, n**2) + 1):
row, column = cells[next]
destination = (board[row][column] if board[row][column] != -1
else next)
if dist[destination] == -1:
dist[destination] = dist[curr] + 1
q.append(destination)
return dist[n * n]
board = [[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1],
[-1, 35, -1, -1, 13, -1],
[-1, -1, -1, -1, -1, -1],
[-1, 15, -1, -1, -1, -1]]