2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2019-07-05 08:48:36 +00:00
|
|
|
|
2024-03-13 06:52:41 +00:00
|
|
|
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.
|
2019-07-05 08:48:36 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2024-03-13 06:52:41 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
from __future__ import annotations
|
2020-11-29 17:30:31 +00:00
|
|
|
|
2019-07-05 08:48:36 +00:00
|
|
|
solution = []
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def is_safe(board: list[list[int]], row: int, column: int) -> bool:
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2020-06-16 08:09:19 +00:00
|
|
|
This function returns a boolean value True if it is safe to place a queen there
|
|
|
|
considering the current state of the board.
|
2019-07-05 08:48:36 +00:00
|
|
|
|
2023-10-22 18:51:56 +00:00
|
|
|
Parameters:
|
|
|
|
board (2D matrix): The chessboard
|
|
|
|
row, column: Coordinates of the cell on the board
|
2019-07-05 08:48:36 +00:00
|
|
|
|
2023-10-22 18:51:56 +00:00
|
|
|
Returns:
|
2019-07-05 08:48:36 +00:00
|
|
|
Boolean Value
|
|
|
|
|
2023-11-27 17:43:51 +00:00
|
|
|
>>> 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
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2023-10-22 18:51:56 +00:00
|
|
|
|
|
|
|
n = len(board) # Size of the board
|
|
|
|
|
|
|
|
# Check if there is any queen in the same row, column,
|
|
|
|
# left upper diagonal, and right upper diagonal
|
|
|
|
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)))
|
|
|
|
)
|
2019-07-05 08:48:36 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def solve(board: list[list[int]], row: int) -> bool:
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2023-10-22 18:51:56 +00:00
|
|
|
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
|
2020-03-04 12:40:28 +00:00
|
|
|
possible solution branch.
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2019-07-05 08:48:36 +00:00
|
|
|
if row >= len(board):
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2023-10-22 18:51:56 +00:00
|
|
|
If the row number exceeds N, we have a board with a successful combination
|
2019-07-05 08:48:36 +00:00
|
|
|
and that combination is appended to the solution list and the board is printed.
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2019-07-05 08:48:36 +00:00
|
|
|
solution.append(board)
|
|
|
|
printboard(board)
|
|
|
|
print()
|
2020-11-29 17:30:31 +00:00
|
|
|
return True
|
2019-07-05 08:48:36 +00:00
|
|
|
for i in range(len(board)):
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2023-10-22 18:51:56 +00:00
|
|
|
For every row, it iterates through each column to check if it is feasible to
|
2020-06-16 08:09:19 +00:00
|
|
|
place a queen there.
|
2023-10-22 18:51:56 +00:00
|
|
|
If all the combinations for that particular branch are successful, the board is
|
2019-07-05 08:48:36 +00:00
|
|
|
reinitialized for the next possible combination.
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2022-10-12 22:54:20 +00:00
|
|
|
if is_safe(board, row, i):
|
2019-07-05 08:48:36 +00:00
|
|
|
board[row][i] = 1
|
2019-10-05 05:14:13 +00:00
|
|
|
solve(board, row + 1)
|
2019-07-05 08:48:36 +00:00
|
|
|
board[row][i] = 0
|
|
|
|
return False
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def printboard(board: list[list[int]]) -> None:
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2020-01-18 12:24:33 +00:00
|
|
|
Prints the boards that have a successful combination.
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2019-07-05 08:48:36 +00:00
|
|
|
for i in range(len(board)):
|
|
|
|
for j in range(len(board)):
|
|
|
|
if board[i][j] == 1:
|
2023-10-22 18:51:56 +00:00
|
|
|
print("Q", end=" ") # Queen is present
|
2019-10-05 05:14:13 +00:00
|
|
|
else:
|
2023-10-22 18:51:56 +00:00
|
|
|
print(".", end=" ") # Empty cell
|
2019-07-05 08:48:36 +00:00
|
|
|
print()
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2023-10-22 18:51:56 +00:00
|
|
|
# Number of queens (e.g., n=8 for an 8x8 board)
|
2019-07-05 08:48:36 +00:00
|
|
|
n = 8
|
2019-10-05 05:14:13 +00:00
|
|
|
board = [[0 for i in range(n)] for j in range(n)]
|
2019-07-05 08:48:36 +00:00
|
|
|
solve(board, 0)
|
2023-10-22 18:51:56 +00:00
|
|
|
print("The total number of solutions are:", len(solution))
|