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 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 issafe0 0 0 0 0 0 0 0 0 1 1 true issafe1 0 0 0 0 0 0 0 0 1 1 false check if there is any queen in the same row column left upper diagonal and right upper diagonal 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 if the row number exceeds n we have a board with a successful combination and that combination is appended to the solution list and the board is printed for every row it iterates through each column to check if it is feasible to place a queen there if all the combinations for that particular branch are successful the board is reinitialized for the next possible combination prints the boards that have a successful combination number of queens e g n8 for an 8x8 board 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 size of the board check if there is any queen in the same row column left upper diagonal and right upper diagonal 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 if the row number exceeds n we have a board with a successful combination and that combination is appended to the solution list and the board is printed for every row it iterates through each column to check if it is feasible to place a queen there if all the combinations for that particular branch are successful the board is reinitialized for the next possible combination prints the boards that have a successful combination queen is present empty cell number of queens e g n 8 for an 8x8 board
from __future__ import annotations

solution = []


def is_safe(board: list[list[int]], row: int, column: int) -> bool:
    

    n = len(board)  

    
    
    return (
        all(board[i][j] != 1 for i, j in zip(range(row, -1, -1), range(column, n)))
        and all(
            board[i][j] != 1 for i, j in zip(range(row, -1, -1), range(column, -1, -1))
        )
        and all(board[i][j] != 1 for i, j in zip(range(row, n), range(column, n)))
        and all(board[i][j] != 1 for i, j in zip(range(row, n), range(column, -1, -1)))
    )


def solve(board: list[list[int]], row: int) -> bool:
    
    if row >= len(board):
        
        solution.append(board)
        printboard(board)
        print()
        return True
    for i in range(len(board)):
        
        if is_safe(board, row, i):
            board[row][i] = 1
            solve(board, row + 1)
            board[row][i] = 0
    return False


def printboard(board: list[list[int]]) -> None:
    
    for i in range(len(board)):
        for j in range(len(board)):
            if board[i][j] == 1:
                print("Q", end=" ")  
            else:
                print(".", end=" ")  
        print()



n = 8
board = [[0 for i in range(n)] for j in range(n)]
solve(board, 0)
print("The total number of solutions are:", len(solution))