9
9
"""
10
10
from __future__ import annotations
11
11
12
+ # Create a list to store solutions
12
13
solution = []
13
14
14
15
@@ -17,50 +18,56 @@ def is_safe(board: list[list[int]], row: int, column: int) -> bool:
17
18
This function returns a boolean value True if it is safe to place a queen there
18
19
considering the current state of the board.
19
20
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
23
24
24
- Returns :
25
+ Returns:
25
26
Boolean Value
26
27
27
28
"""
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 ):
29
34
if board [row ][i ] == 1 :
30
35
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 ):
32
38
if board [i ][column ] == 1 :
33
39
return False
40
+ # Check if there is any queen in the left upper diagonal
34
41
for i , j in zip (range (row , - 1 , - 1 ), range (column , - 1 , - 1 )):
35
42
if board [i ][j ] == 1 :
36
43
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 )):
38
46
if board [i ][j ] == 1 :
39
47
return False
40
48
return True
41
49
42
50
43
51
def solve (board : list [list [int ]], row : int ) -> bool :
44
52
"""
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
46
54
False Boolean and terminates that branch and backtracks to the next
47
55
possible solution branch.
48
56
"""
49
57
if row >= len (board ):
50
58
"""
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
52
60
and that combination is appended to the solution list and the board is printed.
53
-
54
61
"""
55
62
solution .append (board )
56
63
printboard (board )
57
64
print ()
58
65
return True
59
66
for i in range (len (board )):
60
67
"""
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
62
69
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
64
71
reinitialized for the next possible combination.
65
72
"""
66
73
if is_safe (board , row , i ):
@@ -77,14 +84,14 @@ def printboard(board: list[list[int]]) -> None:
77
84
for i in range (len (board )):
78
85
for j in range (len (board )):
79
86
if board [i ][j ] == 1 :
80
- print ("Q" , end = " " )
87
+ print ("Q" , end = " " ) # Queen is present
81
88
else :
82
- print ("." , end = " " )
89
+ print ("." , end = " " ) # Empty cell
83
90
print ()
84
91
85
92
86
- # n=int(input("The no. of queens") )
93
+ # Number of queens (e.g., n=8 for an 8x8 board )
87
94
n = 8
88
95
board = [[0 for i in range (n )] for j in range (n )]
89
96
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