mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-23 22:23:35 +00:00
Enhance readability of N Queens (#9265)
* Enhance readability of N Queens * Simplify is_safe code * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply suggestions from code review --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
This commit is contained in:
parent
6c8743f1e6
commit
a8b94abc8b
@ -18,39 +18,38 @@ def is_safe(board: list[list[int]], row: int, column: int) -> bool:
|
|||||||
considering the current state of the board.
|
considering the current state of the board.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
board(2D matrix) : board
|
board (2D matrix): The chessboard
|
||||||
row ,column : coordinates of the cell on a board
|
row, column: Coordinates of the cell on the board
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Boolean Value
|
Boolean Value
|
||||||
|
|
||||||
"""
|
"""
|
||||||
for i in range(len(board)):
|
|
||||||
if board[row][i] == 1:
|
n = len(board) # Size of the board
|
||||||
return False
|
|
||||||
for i in range(len(board)):
|
# Check if there is any queen in the same row, column,
|
||||||
if board[i][column] == 1:
|
# left upper diagonal, and right upper diagonal
|
||||||
return False
|
return (
|
||||||
for i, j in zip(range(row, -1, -1), range(column, -1, -1)):
|
all(board[i][j] != 1 for i, j in zip(range(row, -1, -1), range(column, n)))
|
||||||
if board[i][j] == 1:
|
and all(
|
||||||
return False
|
board[i][j] != 1 for i, j in zip(range(row, -1, -1), range(column, -1, -1))
|
||||||
for i, j in zip(range(row, -1, -1), range(column, len(board))):
|
)
|
||||||
if board[i][j] == 1:
|
and all(board[i][j] != 1 for i, j in zip(range(row, n), range(column, n)))
|
||||||
return False
|
and all(board[i][j] != 1 for i, j in zip(range(row, n), range(column, -1, -1)))
|
||||||
return True
|
)
|
||||||
|
|
||||||
|
|
||||||
def solve(board: list[list[int]], row: int) -> bool:
|
def solve(board: list[list[int]], row: int) -> bool:
|
||||||
"""
|
"""
|
||||||
It creates a state space tree and calls the safe function until it receives a
|
This function creates a state space tree and calls the safe function until it
|
||||||
False Boolean and terminates that branch and backtracks to the next
|
receives a False Boolean and terminates that branch and backtracks to the next
|
||||||
possible solution branch.
|
possible solution branch.
|
||||||
"""
|
"""
|
||||||
if row >= len(board):
|
if row >= len(board):
|
||||||
"""
|
"""
|
||||||
If the row number exceeds N we have board with a successful combination
|
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.
|
and that combination is appended to the solution list and the board is printed.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
solution.append(board)
|
solution.append(board)
|
||||||
printboard(board)
|
printboard(board)
|
||||||
@ -58,9 +57,9 @@ def solve(board: list[list[int]], row: int) -> bool:
|
|||||||
return True
|
return True
|
||||||
for i in range(len(board)):
|
for i in range(len(board)):
|
||||||
"""
|
"""
|
||||||
For every row it iterates through each column to check if it is feasible to
|
For every row, it iterates through each column to check if it is feasible to
|
||||||
place a queen there.
|
place a queen there.
|
||||||
If all the combinations for that particular branch are successful the board is
|
If all the combinations for that particular branch are successful, the board is
|
||||||
reinitialized for the next possible combination.
|
reinitialized for the next possible combination.
|
||||||
"""
|
"""
|
||||||
if is_safe(board, row, i):
|
if is_safe(board, row, i):
|
||||||
@ -77,14 +76,14 @@ def printboard(board: list[list[int]]) -> None:
|
|||||||
for i in range(len(board)):
|
for i in range(len(board)):
|
||||||
for j in range(len(board)):
|
for j in range(len(board)):
|
||||||
if board[i][j] == 1:
|
if board[i][j] == 1:
|
||||||
print("Q", end=" ")
|
print("Q", end=" ") # Queen is present
|
||||||
else:
|
else:
|
||||||
print(".", end=" ")
|
print(".", end=" ") # Empty cell
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
|
||||||
# n=int(input("The no. of queens"))
|
# Number of queens (e.g., n=8 for an 8x8 board)
|
||||||
n = 8
|
n = 8
|
||||||
board = [[0 for i in range(n)] for j in range(n)]
|
board = [[0 for i in range(n)] for j in range(n)]
|
||||||
solve(board, 0)
|
solve(board, 0)
|
||||||
print("The total no. of solutions are :", len(solution))
|
print("The total number of solutions are:", len(solution))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user