Skip to content

Latest commit

 

History

History
63 lines (45 loc) · 1.64 KB

_909. Snakes and Ladders.md

File metadata and controls

63 lines (45 loc) · 1.64 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 12, 2024

Last updated : July 01, 2024


Related Topics : Array, Breadth-First Search, Matrix

Acceptance Rate : 43.83 %


Solutions

Python

# I hate this question so much sigh. The way it's worded is terrible.

class Solution:
    def snakesAndLadders(self, board: List[List[int]]) -> int:
        linearBoard = [0]

        for i in range(len(board)) :
            if i % 2 == 0 :
                linearBoard.extend(board[len(board) - i - 1])
            else :
                linearBoard.extend(reversed(board[len(board) - i - 1]))

        toVisit = deque([(0, 1)])
        visited = set()

        while toVisit :
            steps, indx = toVisit.popleft()
            steps += 1

            if indx + 6 >= len(board) * len(board[0]) :
                return steps

            for i in range(indx + 1, indx + 7) :
                if i in visited :
                    continue
                visited.add(i)

                if linearBoard[i] != -1 :
                    if linearBoard[i] == len(board) * len(board[0]) :
                        return steps
                    toVisit.append((steps, linearBoard[i]))
                else :
                    toVisit.append((steps, i))

        return -1