backtracking.knight_tour

Functions

get_valid_pos(→ list[tuple[int, int]])

Find all the valid positions a knight can move to from the current position.

is_complete(→ bool)

Check if the board (matrix) has been completely filled with non-zero values.

open_knight_tour(→ list[list[int]])

Find the solution for the knight tour problem for a board of size n. Raises

open_knight_tour_helper(→ bool)

Helper function to solve knight tour problem.

Module Contents

backtracking.knight_tour.get_valid_pos(position: tuple[int, int], n: int) list[tuple[int, int]]

Find all the valid positions a knight can move to from the current position.

>>> get_valid_pos((1, 3), 4)
[(2, 1), (0, 1), (3, 2)]
backtracking.knight_tour.is_complete(board: list[list[int]]) bool

Check if the board (matrix) has been completely filled with non-zero values.

>>> is_complete([[1]])
True
>>> is_complete([[1, 2], [3, 0]])
False
backtracking.knight_tour.open_knight_tour(n: int) list[list[int]]

Find the solution for the knight tour problem for a board of size n. Raises ValueError if the tour cannot be performed for the given size.

>>> open_knight_tour(1)
[[1]]
>>> open_knight_tour(2)
Traceback (most recent call last):
    ...
ValueError: Open Knight Tour cannot be performed on a board of size 2
backtracking.knight_tour.open_knight_tour_helper(board: list[list[int]], pos: tuple[int, int], curr: int) bool

Helper function to solve knight tour problem.