@@ -17,50 +17,49 @@ def is_safe(board: list[list[int]], row: int, column: int) -> bool:
17
17
This function returns a boolean value True if it is safe to place a queen there
18
18
considering the current state of the board.
19
19
20
- Parameters :
21
- board(2D matrix) : board
22
- row ,column : coordinates of the cell on a board
20
+ Parameters:
21
+ board (2D matrix): The chessboard
22
+ row, column: Coordinates of the cell on the board
23
23
24
- Returns :
24
+ Returns:
25
25
Boolean Value
26
26
27
27
"""
28
- for i in range ( len ( board )):
29
- if board [ row ][ i ] == 1 :
30
- return False
31
- for i in range ( len ( board )):
32
- if board [ i ][ column ] == 1 :
33
- return False
34
- for i , j in zip (range (row , - 1 , - 1 ), range (column , - 1 , - 1 )):
35
- if board [ i ][ j ] == 1 :
36
- return False
37
- for i , j in zip ( range ( row , - 1 , - 1 ), range ( column , len ( board ))):
38
- if board [i ][j ] == 1 :
39
- return False
40
- return True
28
+
29
+ n = len ( board ) # Size of the board
30
+
31
+ # Check if there is any queen in the same row, column,
32
+ # left upper diagonal, and right upper diagonal
33
+ return (
34
+ all ( board [ i ][ j ] != 1 for i , j in zip (range (row , - 1 , - 1 ), range (column , n )))
35
+ and all (
36
+ board [ i ][ j ] != 1 for i , j in zip ( range ( row , - 1 , - 1 ), range ( column , - 1 , - 1 ))
37
+ )
38
+ and all ( board [i ][j ] != 1 for i , j in zip ( range ( row , n ), range ( column , n )))
39
+ and all ( board [ i ][ j ] != 1 for i , j in zip ( range ( row , n ), range ( column , - 1 , - 1 )))
40
+ )
41
41
42
42
43
43
def solve (board : list [list [int ]], row : int ) -> bool :
44
44
"""
45
- It creates a state space tree and calls the safe function until it receives a
46
- False Boolean and terminates that branch and backtracks to the next
45
+ This function creates a state space tree and calls the safe function until it
46
+ receives a False Boolean and terminates that branch and backtracks to the next
47
47
possible solution branch.
48
48
"""
49
49
if row >= len (board ):
50
50
"""
51
- If the row number exceeds N we have board with a successful combination
51
+ If the row number exceeds N, we have a board with a successful combination
52
52
and that combination is appended to the solution list and the board is printed.
53
-
54
53
"""
55
54
solution .append (board )
56
55
printboard (board )
57
56
print ()
58
57
return True
59
58
for i in range (len (board )):
60
59
"""
61
- For every row it iterates through each column to check if it is feasible to
60
+ For every row, it iterates through each column to check if it is feasible to
62
61
place a queen there.
63
- If all the combinations for that particular branch are successful the board is
62
+ If all the combinations for that particular branch are successful, the board is
64
63
reinitialized for the next possible combination.
65
64
"""
66
65
if is_safe (board , row , i ):
@@ -77,14 +76,14 @@ def printboard(board: list[list[int]]) -> None:
77
76
for i in range (len (board )):
78
77
for j in range (len (board )):
79
78
if board [i ][j ] == 1 :
80
- print ("Q" , end = " " )
79
+ print ("Q" , end = " " ) # Queen is present
81
80
else :
82
- print ("." , end = " " )
81
+ print ("." , end = " " ) # Empty cell
83
82
print ()
84
83
85
84
86
- # n=int(input("The no. of queens") )
85
+ # Number of queens (e.g., n=8 for an 8x8 board )
87
86
n = 8
88
87
board = [[0 for i in range (n )] for j in range (n )]
89
88
solve (board , 0 )
90
- print ("The total no. of solutions are :" , len (solution ))
89
+ print ("The total number of solutions are:" , len (solution ))
0 commit comments