diff --git a/909-Snakes-and-Ladders.py b/909-Snakes-and-Ladders.py new file mode 100644 index 000000000..b2338d7f0 --- /dev/null +++ b/909-Snakes-and-Ladders.py @@ -0,0 +1,27 @@ +class Solution: + def snakesAndLadders(self, board: List[List[int]]) -> int: + length = len(board) + board.reverse() + def intToPos(square): + r = (square - 1) // length + c = (square - 1) % length + if r % 2: + c = length - 1 - c + return [r, c] + + q = deque() + q.append([1, 0]) # [square, moves] + visit = set() + while q: + square, moves = q.popleft() + for i in range(1, 7): + nextSquare = square + i + r, c = intToPos(nextSquare) + if board[r][c] != -1: + nextSquare = board[r][c] + if nextSquare == length * length: + return moves + 1 + if nextSquare not in visit: + visit.add(nextSquare) + q.append([nextSquare, moves + 1]) + return -1