Skip to content

Update n_queens_math.py #11767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions backtracking/n_queens_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,31 @@

"""

from __future__ import annotations


from typing import List

Check failure on line 82 in backtracking/n_queens_math.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

backtracking/n_queens_math.py:82:1: UP035 `typing.List` is deprecated, use `list` instead


def depth_first_search(

Check failure on line 85 in backtracking/n_queens_math.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

backtracking/n_queens_math.py:79:1: I001 Import block is un-sorted or un-formatted
possible_board: list[int],
diagonal_right_collisions: list[int],
diagonal_left_collisions: list[int],
boards: list[list[str]],
possible_board: List[int],

Check failure on line 86 in backtracking/n_queens_math.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/n_queens_math.py:86:21: UP006 Use `list` instead of `List` for type annotation
diagonal_right_collisions: List[int],

Check failure on line 87 in backtracking/n_queens_math.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/n_queens_math.py:87:32: UP006 Use `list` instead of `List` for type annotation
diagonal_left_collisions: List[int],

Check failure on line 88 in backtracking/n_queens_math.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/n_queens_math.py:88:31: UP006 Use `list` instead of `List` for type annotation
boards: List[List[str]],

Check failure on line 89 in backtracking/n_queens_math.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/n_queens_math.py:89:13: UP006 Use `list` instead of `List` for type annotation

Check failure on line 89 in backtracking/n_queens_math.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/n_queens_math.py:89:18: UP006 Use `list` instead of `List` for type annotation
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
Expand Down Expand Up @@ -129,14 +137,25 @@
continue

# 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)

# Recursive call with updated lists
depth_first_search(
[*possible_board, col],
[*diagonal_right_collisions, row - col],
[*diagonal_left_collisions, row + col],
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:
boards: list[list[str]] = []
Expand Down
Loading