Skip to content

Commit 0d5ce7b

Browse files
Hardvanpre-commit-ci[bot]tianyizheng02
authored andcommitted
Enhance readability of N Queens (TheAlgorithms#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>
1 parent cd4633e commit 0d5ce7b

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

backtracking/n_queens.py

+26-27
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,49 @@ def is_safe(board: list[list[int]], row: int, column: int) -> bool:
1717
This function returns a boolean value True if it is safe to place a queen there
1818
considering the current state of the board.
1919
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
2323
24-
Returns :
24+
Returns:
2525
Boolean Value
2626
2727
"""
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+
)
4141

4242

4343
def solve(board: list[list[int]], row: int) -> bool:
4444
"""
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
4747
possible solution branch.
4848
"""
4949
if row >= len(board):
5050
"""
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
5252
and that combination is appended to the solution list and the board is printed.
53-
5453
"""
5554
solution.append(board)
5655
printboard(board)
5756
print()
5857
return True
5958
for i in range(len(board)):
6059
"""
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
6261
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
6463
reinitialized for the next possible combination.
6564
"""
6665
if is_safe(board, row, i):
@@ -77,14 +76,14 @@ def printboard(board: list[list[int]]) -> None:
7776
for i in range(len(board)):
7877
for j in range(len(board)):
7978
if board[i][j] == 1:
80-
print("Q", end=" ")
79+
print("Q", end=" ") # Queen is present
8180
else:
82-
print(".", end=" ")
81+
print(".", end=" ") # Empty cell
8382
print()
8483

8584

86-
# n=int(input("The no. of queens"))
85+
# Number of queens (e.g., n=8 for an 8x8 board)
8786
n = 8
8887
board = [[0 for i in range(n)] for j in range(n)]
8988
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

Comments
 (0)