7575for another one or vice versa.
7676
7777"""
78- from __future__ import annotations
78+ from typing import List
7979
8080
8181def depth_first_search (
82- possible_board : list [int ],
83- diagonal_right_collisions : list [int ],
84- diagonal_left_collisions : list [int ],
85- boards : list [ list [str ]],
82+ possible_board : List [int ],
83+ diagonal_right_collisions : List [int ],
84+ diagonal_left_collisions : List [int ],
85+ boards : List [ List [str ]],
8686 n : int ,
8787) -> None :
8888 """
@@ -94,48 +94,41 @@ def depth_first_search(
9494 ['. . Q . ', 'Q . . . ', '. . . Q ', '. Q . . ']
9595 """
9696
97- """ Get next row in the current board (possible_board) to fill it with a queen """
97+ # Get next row in the current board (possible_board) to fill it with a queen
9898 row = len (possible_board )
9999
100- """
101- If row is equal to the size of the board it means there are a queen in each row in
102- the current board (possible_board)
103- """
100+ # If row is equal to the size of the board it means there are a queen in each row in
101+ # the current board (possible_board)
104102 if row == n :
105- """
106- We convert the variable possible_board that looks like this: [1, 3, 0, 2] to
107- this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . ']
108- """
109- possible_board = [". " * i + "Q " + ". " * (n - 1 - i ) for i in possible_board ]
110- boards .append (possible_board )
103+ # We convert the variable possible_board that looks like this: [1, 3, 0, 2] to
104+ # this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . ']
105+ boards .append ([". " * i + "Q " + ". " * (n - 1 - i ) for i in possible_board ])
111106 return
112107
113- """ We iterate each column in the row to find all possible results in each row """
108+ # We iterate each column in the row to find all possible results in each row
114109 for col in range (n ):
115110
116- """
117- We apply that we learned previously. First we check that in the current board
118- (possible_board) there are not other same value because if there is it means
119- that there are a collision in vertical. Then we apply the two formulas we
120- learned before:
121-
122- 45º: y - x = b or 45: row - col = b
123- 135º: y + x = b or row + col = b.
124-
125- And we verify if the results of this two formulas not exist in their variables
126- respectively. (diagonal_right_collisions, diagonal_left_collisions)
127-
128- If any or these are True it means there is a collision so we continue to the
129- next value in the for loop.
130- """
111+ # We apply that we learned previously. First we check that in the current board
112+ # (possible_board) there are not other same value because if there is it means
113+ # that there are a collision in vertical. Then we apply the two formulas we
114+ # learned before:
115+ #
116+ # 45º: y - x = b or 45: row - col = b
117+ # 135º: y + x = b or row + col = b.
118+ #
119+ # And we verify if the results of this two formulas not exist in their variables
120+ # respectively. (diagonal_right_collisions, diagonal_left_collisions)
121+ #
122+ # If any or these are True it means there is a collision so we continue to the
123+ # next value in the for loop.
131124 if (
132125 col in possible_board
133126 or row - col in diagonal_right_collisions
134127 or row + col in diagonal_left_collisions
135128 ):
136129 continue
137130
138- """ If it is False we call dfs function again and we update the inputs """
131+ # If it is False we call dfs function again and we update the inputs
139132 depth_first_search (
140133 possible_board + [col ],
141134 diagonal_right_collisions + [row - col ],
@@ -146,10 +139,10 @@ def depth_first_search(
146139
147140
148141def n_queens_solution (n : int ) -> None :
149- boards = []
142+ boards : List [ List [ str ]] = []
150143 depth_first_search ([], [], [], boards , n )
151144
152- """ Print all the boards """
145+ # Print all the boards
153146 for board in boards :
154147 for column in board :
155148 print (column )
0 commit comments