Skip to content

Commit a1853d6

Browse files
committed
Enhance readability of N Queens
1 parent eaa87bd commit a1853d6

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

Diff for: backtracking/n_queens.py

+23-16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010
from __future__ import annotations
1111

12+
# Create a list to store solutions
1213
solution = []
1314

1415

@@ -17,50 +18,56 @@ def is_safe(board: list[list[int]], row: int, column: int) -> bool:
1718
This function returns a boolean value True if it is safe to place a queen there
1819
considering the current state of the board.
1920
20-
Parameters :
21-
board(2D matrix) : board
22-
row ,column : coordinates of the cell on a board
21+
Parameters:
22+
board (2D matrix): The chessboard
23+
row, column: Coordinates of the cell on the board
2324
24-
Returns :
25+
Returns:
2526
Boolean Value
2627
2728
"""
28-
for i in range(len(board)):
29+
30+
n = len(board) # Size of the board
31+
32+
# Check if there is any queen in the same row
33+
for i in range(n):
2934
if board[row][i] == 1:
3035
return False
31-
for i in range(len(board)):
36+
# Check if there is any queen in the same column
37+
for i in range(n):
3238
if board[i][column] == 1:
3339
return False
40+
# Check if there is any queen in the left upper diagonal
3441
for i, j in zip(range(row, -1, -1), range(column, -1, -1)):
3542
if board[i][j] == 1:
3643
return False
37-
for i, j in zip(range(row, -1, -1), range(column, len(board))):
44+
# Check if there is any queen in the right upper diagonal
45+
for i, j in zip(range(row, -1, -1), range(column, n)):
3846
if board[i][j] == 1:
3947
return False
4048
return True
4149

4250

4351
def solve(board: list[list[int]], row: int) -> bool:
4452
"""
45-
It creates a state space tree and calls the safe function until it receives a
53+
This function creates a state space tree and calls the safe function until it receives a
4654
False Boolean and terminates that branch and backtracks to the next
4755
possible solution branch.
4856
"""
4957
if row >= len(board):
5058
"""
51-
If the row number exceeds N we have board with a successful combination
59+
If the row number exceeds N, we have a board with a successful combination
5260
and that combination is appended to the solution list and the board is printed.
53-
5461
"""
5562
solution.append(board)
5663
printboard(board)
5764
print()
5865
return True
5966
for i in range(len(board)):
6067
"""
61-
For every row it iterates through each column to check if it is feasible to
68+
For every row, it iterates through each column to check if it is feasible to
6269
place a queen there.
63-
If all the combinations for that particular branch are successful the board is
70+
If all the combinations for that particular branch are successful, the board is
6471
reinitialized for the next possible combination.
6572
"""
6673
if is_safe(board, row, i):
@@ -77,14 +84,14 @@ def printboard(board: list[list[int]]) -> None:
7784
for i in range(len(board)):
7885
for j in range(len(board)):
7986
if board[i][j] == 1:
80-
print("Q", end=" ")
87+
print("Q", end=" ") # Queen is present
8188
else:
82-
print(".", end=" ")
89+
print(".", end=" ") # Empty cell
8390
print()
8491

8592

86-
# n=int(input("The no. of queens"))
93+
# Number of queens (e.g., n=8 for an 8x8 board)
8794
n = 8
8895
board = [[0 for i in range(n)] for j in range(n)]
8996
solve(board, 0)
90-
print("The total no. of solutions are :", len(solution))
97+
print("The total number of solutions are:", len(solution))

0 commit comments

Comments
 (0)