From 040437ae3907547baff69124f5feda99e60cc291 Mon Sep 17 00:00:00 2001 From: Shanvi Shukla <103589784+Shanvithegreat0@users.noreply.github.com> Date: Sat, 5 Oct 2024 10:11:13 +0530 Subject: [PATCH 1/3] Update n_queens_math.py The first change reduces the memory overhead associated with creating new lists at each recursive step by modifying the same list and backtracking after each recursive call. This can make a significant difference in memory usage for large boards. The second change uses List from typing for better type hinting. Also, it clarifies the function's purpose with a more descriptive docstring --- backtracking/n_queens_math.py | 47 ++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/backtracking/n_queens_math.py b/backtracking/n_queens_math.py index 287d1f090373..e85073605d13 100644 --- a/backtracking/n_queens_math.py +++ b/backtracking/n_queens_math.py @@ -79,20 +79,27 @@ from __future__ import annotations +from typing import List + def depth_first_search( - possible_board: list[int], - diagonal_right_collisions: list[int], - diagonal_left_collisions: list[int], - boards: list[list[str]], + possible_board: List[int], + diagonal_right_collisions: List[int], + diagonal_left_collisions: List[int], + boards: List[List[str]], n: int, ) -> None: """ - >>> boards = [] - >>> depth_first_search([], [], [], boards, 4) - >>> for board in boards: - ... print(board) - ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . '] - ['. . Q . ', 'Q . . . ', '. . . Q ', '. Q . . '] + Recursively search for all possible solutions to the N-Queens problem. + + Parameters: + - possible_board: List representing the columns occupied by queens. + - diagonal_right_collisions: List to track collisions in the right diagonal. + - diagonal_left_collisions: List to track collisions in the left diagonal. + - boards: List to store valid N-Queens board configurations. + - n: Size of the chessboard (N x N). + + Returns: + None """ # Get next row in the current board (possible_board) to fill it with a queen @@ -129,13 +136,19 @@ def depth_first_search( continue # If it is False we call dfs function again and we update the inputs - depth_first_search( - [*possible_board, col], - [*diagonal_right_collisions, row - col], - [*diagonal_left_collisions, row + col], - boards, - n, - ) + # Add new values to the current lists + possible_board.append(col) + diagonal_right_collisions.append(row - col) + diagonal_left_collisions.append(row + col) + + # Recursive call with updated lists + depth_first_search(possible_board, diagonal_right_collisions, diagonal_left_collisions, boards, n) + + # Backtrack by removing the last added values after returning from recursion + possible_board.pop() + diagonal_right_collisions.pop() + diagonal_left_collisions.pop() + def n_queens_solution(n: int) -> None: From a9eb6de413d033046c957b91ce7192aac30bb904 Mon Sep 17 00:00:00 2001 From: Shanvi Shukla <103589784+Shanvithegreat0@users.noreply.github.com> Date: Sat, 5 Oct 2024 10:17:25 +0530 Subject: [PATCH 2/3] Update n_queens_math.py --- backtracking/n_queens_math.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/backtracking/n_queens_math.py b/backtracking/n_queens_math.py index e85073605d13..f6fe551ddb56 100644 --- a/backtracking/n_queens_math.py +++ b/backtracking/n_queens_math.py @@ -137,12 +137,12 @@ def depth_first_search( # If it is False we call dfs function again and we update the inputs # Add new values to the current lists - possible_board.append(col) - diagonal_right_collisions.append(row - col) - diagonal_left_collisions.append(row + col) + possible_board.append(col) + diagonal_right_collisions.append(row - col) + diagonal_left_collisions.append(row + col) # Recursive call with updated lists - depth_first_search(possible_board, diagonal_right_collisions, diagonal_left_collisions, boards, n) + depth_first_search(possible_board, diagonal_right_collisions, diagonal_left_collisions, boards, n) # Backtrack by removing the last added values after returning from recursion possible_board.pop() From dca7e2ae51132e3c483e09a55c5905fa70161a14 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 5 Oct 2024 04:47:47 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/n_queens_math.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/backtracking/n_queens_math.py b/backtracking/n_queens_math.py index f6fe551ddb56..21539c459fa9 100644 --- a/backtracking/n_queens_math.py +++ b/backtracking/n_queens_math.py @@ -81,6 +81,7 @@ from typing import List + def depth_first_search( possible_board: List[int], diagonal_right_collisions: List[int], @@ -141,16 +142,21 @@ def depth_first_search( diagonal_right_collisions.append(row - col) diagonal_left_collisions.append(row + col) - # Recursive call with updated lists - depth_first_search(possible_board, diagonal_right_collisions, diagonal_left_collisions, boards, n) + # Recursive call with updated lists + depth_first_search( + possible_board, + diagonal_right_collisions, + diagonal_left_collisions, + boards, + n, + ) - # Backtrack by removing the last added values after returning from recursion + # Backtrack by removing the last added values after returning from recursion possible_board.pop() diagonal_right_collisions.pop() diagonal_left_collisions.pop() - def n_queens_solution(n: int) -> None: boards: list[list[str]] = [] depth_first_search([], [], [], boards, n)