backtracking.n_queens

The nqueens problem is of placing N queens on a N * N chess board such that no queen can attack any other queens placed on that chess board. This means that one queen cannot have any other queen on its horizontal, vertical and diagonal lines.

Attributes

board

n

solution

Functions

is_safe(→ bool)

This function returns a boolean value True if it is safe to place a queen there

printboard(→ None)

Prints the boards that have a successful combination.

solve(→ bool)

This function creates a state space tree and calls the safe function until it

Module Contents

backtracking.n_queens.is_safe(board: list[list[int]], row: int, column: int) bool

This function returns a boolean value True if it is safe to place a queen there considering the current state of the board.

Parameters: board (2D matrix): The chessboard row, column: Coordinates of the cell on the board

Returns: Boolean Value

>>> is_safe([[0, 0, 0], [0, 0, 0], [0, 0, 0]], 1, 1)
True
>>> is_safe([[1, 0, 0], [0, 0, 0], [0, 0, 0]], 1, 1)
False
backtracking.n_queens.printboard(board: list[list[int]]) None

Prints the boards that have a successful combination.

backtracking.n_queens.solve(board: list[list[int]], row: int) bool

This function creates a state space tree and calls the safe function until it receives a False Boolean and terminates that branch and backtracks to the next possible solution branch.

backtracking.n_queens.board
backtracking.n_queens.n = 8
backtracking.n_queens.solution = []