51. N-Queens Brute force

class Solution: def solveNQueens(self, n): """ Loop through all rows Every solution is represented by a list The list index represent rows. """ res = ['.'] * n # there are n positions, currently all unfilled success = [] # stores all success histories history = [] # stores a single history # consider the first queen at first row: # value will iterate from 0 to n-1 positions = get_all_positions(history) if positions.is_empty(): continue for pos in positions: place queen at pos go to second row: positions = get_all_positions(history) if positions.is_empty(): continue else for pos in positions: place queen at pos go to the third row positions = get_all_positions(history) if positions.is_empty(): continue
Brute force

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.