讨论/《广度优先搜索》 - 练习:蛇梯棋/
《广度优先搜索》 - 练习:蛇梯棋
class Solution:
    def snakesAndLadders(self, board: List[List[int]]) -> int:
        n = len(board)
        indx_board = [(0,0)]
        for i in range(n):
            for j in range(n):
                x = n-i-1
                if i % 2 == 0:
                    y = j
                else:
                    y = n-j-1
                indx_board.append((x,y))

        que = collections.deque()
        que.append(1)
        visited = set()
        visited.add(1)
        step = 0
        while que:
            level_len = len(que)
            step += 1
            for k in range(level_len):
                now = que.popleft()
                for xx in range(now+1, min(now+7,n*n+1)):
                    if board[indx_board[xx][0]][indx_board[xx][1]] != -1:
                        to =  board[indx_board[xx][0]][indx_board[xx][1]]
                    else:
                        to = xx
                    if to not in visited:
                        visited.add(to)
                        que.append(to)
                    if to == n*n:
                        return step
        return -1