From a1853d66c89d7fcf1e0445249ec44129c50248ea Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 1 Oct 2023 16:22:44 +0530 Subject: [PATCH 1/4] Enhance readability of N Queens --- backtracking/n_queens.py | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/backtracking/n_queens.py b/backtracking/n_queens.py index bbf0ce44f91c..f0daf59830dd 100644 --- a/backtracking/n_queens.py +++ b/backtracking/n_queens.py @@ -9,6 +9,7 @@ """ from __future__ import annotations +# Create a list to store solutions solution = [] @@ -17,24 +18,31 @@ def is_safe(board: list[list[int]], row: int, column: int) -> bool: This function returns a boolean value True if it is safe to place a queen there considering the current state of the board. - Parameters : - board(2D matrix) : board - row ,column : coordinates of the cell on a board + Parameters: + board (2D matrix): The chessboard + row, column: Coordinates of the cell on the board - Returns : + Returns: Boolean Value """ - for i in range(len(board)): + + n = len(board) # Size of the board + + # Check if there is any queen in the same row + for i in range(n): if board[row][i] == 1: return False - for i in range(len(board)): + # Check if there is any queen in the same column + for i in range(n): if board[i][column] == 1: return False + # Check if there is any queen in the left upper diagonal for i, j in zip(range(row, -1, -1), range(column, -1, -1)): if board[i][j] == 1: return False - for i, j in zip(range(row, -1, -1), range(column, len(board))): + # Check if there is any queen in the right upper diagonal + for i, j in zip(range(row, -1, -1), range(column, n)): if board[i][j] == 1: return False return True @@ -42,15 +50,14 @@ def is_safe(board: list[list[int]], row: int, column: 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 receives a False Boolean and terminates that branch and backtracks to the next possible solution branch. """ 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. - """ solution.append(board) printboard(board) @@ -58,9 +65,9 @@ def solve(board: list[list[int]], row: int) -> bool: return True 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. - 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. """ if is_safe(board, row, i): @@ -77,14 +84,14 @@ def printboard(board: list[list[int]]) -> None: for i in range(len(board)): for j in range(len(board)): if board[i][j] == 1: - print("Q", end=" ") + print("Q", end=" ") # Queen is present else: - print(".", end=" ") + print(".", end=" ") # Empty cell print() -# n=int(input("The no. of queens")) +# Number of queens (e.g., n=8 for an 8x8 board) n = 8 board = [[0 for i in range(n)] for j in range(n)] solve(board, 0) -print("The total no. of solutions are :", len(solution)) +print("The total number of solutions are:", len(solution)) From 631a3ced8a3c96117b6f6bce50a2f9df97b5eecc Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 1 Oct 2023 16:29:21 +0530 Subject: [PATCH 2/4] Simplify is_safe code --- backtracking/n_queens.py | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/backtracking/n_queens.py b/backtracking/n_queens.py index f0daf59830dd..8aa0ce1e9ba7 100644 --- a/backtracking/n_queens.py +++ b/backtracking/n_queens.py @@ -26,32 +26,21 @@ def is_safe(board: list[list[int]], row: int, column: int) -> bool: Boolean Value """ - - n = len(board) # Size of the board - - # Check if there is any queen in the same row - for i in range(n): - if board[row][i] == 1: - return False - # Check if there is any queen in the same column - for i in range(n): - if board[i][column] == 1: - return False - # Check if there is any queen in the left upper diagonal - for i, j in zip(range(row, -1, -1), range(column, -1, -1)): - if board[i][j] == 1: - return False - # Check if there is any queen in the right upper diagonal - for i, j in zip(range(row, -1, -1), range(column, n)): - if board[i][j] == 1: - return False - return True + + n = len(board) # Size of the board + + # Check if there is any queen in the same row, column, + # left upper diagonal, and right upper diagonal + return all(board[i][j] != 1 for i, j in zip(range(row, -1, -1), range(column, n))) \ + and all(board[i][j] != 1 for i, j in zip(range(row, -1, -1), range(column, -1, -1))) \ + and all(board[i][j] != 1 for i, j in zip(range(row, n), range(column, n))) \ + and all(board[i][j] != 1 for i, j in zip(range(row, n), range(column, -1, -1))) def solve(board: list[list[int]], row: int) -> bool: """ - This function creates a state space tree and calls the safe function until it receives a - False Boolean and terminates that branch and backtracks to the next + This function creates a state space tree and calls the safe function until it + receives a False Boolean and terminates that branch and backtracks to the next possible solution branch. """ if row >= len(board): From 93937d465de6cdc8a0bcf7ab2f0dd38ac20bc137 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:00:51 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/n_queens.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/backtracking/n_queens.py b/backtracking/n_queens.py index 8aa0ce1e9ba7..f7d120d6b7a9 100644 --- a/backtracking/n_queens.py +++ b/backtracking/n_queens.py @@ -31,10 +31,14 @@ def is_safe(board: list[list[int]], row: int, column: int) -> bool: # Check if there is any queen in the same row, column, # left upper diagonal, and right upper diagonal - return all(board[i][j] != 1 for i, j in zip(range(row, -1, -1), range(column, n))) \ - and all(board[i][j] != 1 for i, j in zip(range(row, -1, -1), range(column, -1, -1))) \ - and all(board[i][j] != 1 for i, j in zip(range(row, n), range(column, n))) \ + return ( + all(board[i][j] != 1 for i, j in zip(range(row, -1, -1), range(column, n))) + and all( + board[i][j] != 1 for i, j in zip(range(row, -1, -1), range(column, -1, -1)) + ) + and all(board[i][j] != 1 for i, j in zip(range(row, n), range(column, n))) and all(board[i][j] != 1 for i, j in zip(range(row, n), range(column, -1, -1))) + ) def solve(board: list[list[int]], row: int) -> bool: From 374faca24b9830f15e2feb0fe96b97304d2127f8 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sun, 22 Oct 2023 14:44:26 -0400 Subject: [PATCH 4/4] Apply suggestions from code review --- backtracking/n_queens.py | 1 - 1 file changed, 1 deletion(-) diff --git a/backtracking/n_queens.py b/backtracking/n_queens.py index f7d120d6b7a9..0f237d95e7c8 100644 --- a/backtracking/n_queens.py +++ b/backtracking/n_queens.py @@ -9,7 +9,6 @@ """ from __future__ import annotations -# Create a list to store solutions solution = []