





from heapq import heappop, heappush

import numpy as np



def 				lowerCAmelCase_ (							A_    ,A_    ,A_    ,A_    ,):
    UpperCamelCase__				, UpperCamelCase__:	int					=     grid.shape
    UpperCamelCase__:	Any					=     [-1, 1, 0, 0]
    UpperCamelCase__:	int					=     [0, 0, -1, 1]
    if allow_diagonal:
        dx += [-1, -1, 1, 1]
        dy += [-1, 1, -1, 1]

    UpperCamelCase__				, UpperCamelCase__:	Union[str, Any]					=     [(0, source)], set()
    UpperCamelCase__:	List[str]					=     np.full((rows, cols)    ,np.inf)
    UpperCamelCase__:	Union[str, Any]					=     0
    UpperCamelCase__:	Optional[Any]					=     np.empty((rows, cols)    ,dtype=A_)
    UpperCamelCase__:	List[str]					=     None

    while queue:
        ((UpperCamelCase__)				, (UpperCamelCase__)):	str					=     heappop(A_)
        if (x, y) in visited:
            continue
        visited.add((x, y))

        if (x, y) == destination:
            UpperCamelCase__:	Tuple					=     []
            while (x, y) != source:
                path.append((x, y))
                UpperCamelCase__				, UpperCamelCase__:	str					=     predecessors[x, y]
            path.append(A_)  # add the source manually
            path.reverse()
            return matrix[destination], path

        for i in range(len(A_)):
            UpperCamelCase__				, UpperCamelCase__:	Any					=     x + dx[i], y + dy[i]
            if 0 <= nx < rows and 0 <= ny < cols:
                UpperCamelCase__:	Union[str, Any]					=     grid[nx][ny]
                if next_node == 1 and matrix[nx, ny] > dist + 1:
                    heappush(A_    ,(dist + 1, (nx, ny)))
                    UpperCamelCase__:	Optional[Any]					=     dist + 1
                    UpperCamelCase__:	Tuple					=     (x, y)

    return np.inf, []


if __name__ == "__main__":
   import doctest

   doctest.testmod()








def 				lowerCAmelCase_ (							A_    ,A_):
    UpperCamelCase__:	str					=     [1]
    for i in range(2    ,A_):
        factorials.append(factorials[-1] * i)
    assert 0 <= k < factorials[-1] * n, "k out of bounds"

    UpperCamelCase__:	Optional[int]					=     []
    UpperCamelCase__:	Optional[Any]					=     list(range(A_))

    # Find permutation
    while factorials:
        UpperCamelCase__:	Dict					=     factorials.pop()
        UpperCamelCase__				, UpperCamelCase__:	Optional[int]					=     divmod(A_    ,A_)
        permutation.append(elements[number])
        elements.remove(elements[number])
    permutation.append(elements[0])

    return permutation


if __name__ == "__main__":
   import doctest

   doctest.testmod()

