2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2019-07-05 08:48:36 +00:00
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
The nqueens problem is of placing N queens on a N * N
|
2019-07-05 08:48:36 +00:00
|
|
|
chess board such that no queen can attack any other queens placed
|
|
|
|
on that chess board.
|
2020-05-22 06:10:11 +00:00
|
|
|
This means that one queen cannot have any other queen on its horizontal, vertical and
|
2019-07-05 08:48:36 +00:00
|
|
|
diagonal lines.
|
|
|
|
|
2019-10-05 05:14:13 +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
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def isSafe(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
|
|
|
|
|
|
|
Parameters :
|
|
|
|
board(2D matrix) : board
|
|
|
|
row ,column : coordinates of the cell on a board
|
|
|
|
|
|
|
|
Returns :
|
|
|
|
Boolean Value
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2019-07-05 08:48:36 +00:00
|
|
|
for i in range(len(board)):
|
|
|
|
if board[row][i] == 1:
|
|
|
|
return False
|
|
|
|
for i in range(len(board)):
|
|
|
|
if board[i][column] == 1:
|
|
|
|
return False
|
2019-10-05 05:14:13 +00:00
|
|
|
for i, j in zip(range(row, -1, -1), range(column, -1, -1)):
|
2019-07-05 08:48:36 +00:00
|
|
|
if board[i][j] == 1:
|
|
|
|
return False
|
2019-10-05 05:14:13 +00:00
|
|
|
for i, j in zip(range(row, -1, -1), range(column, len(board))):
|
2019-07-05 08:48:36 +00:00
|
|
|
if board[i][j] == 1:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
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
|
|
|
"""
|
2020-05-22 06:10:11 +00:00
|
|
|
It 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
|
|
|
"""
|
2020-05-22 06:10:11 +00:00
|
|
|
If the row number exceeds N we have 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
|
|
|
"""
|
2020-06-16 08:09:19 +00:00
|
|
|
For every row it iterates through each column to check if it is feasible to
|
|
|
|
place a queen there.
|
2020-05-22 06:10:11 +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
|
|
|
"""
|
|
|
|
if isSafe(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:
|
2019-10-05 05:14:13 +00:00
|
|
|
print("Q", end=" ")
|
|
|
|
else:
|
|
|
|
print(".", end=" ")
|
2019-07-05 08:48:36 +00:00
|
|
|
print()
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
# n=int(input("The no. of queens"))
|
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)
|
|
|
|
print("The total no. of solutions are :", len(solution))
|