Title: Given an integer n , returns the n number of different solutions of the queen.
Source: https: //leetcode-cn.com/problems/n-queens-ii/
Act One: your own code more than 90 percent
Ideas: calculated odd and even, respectively, the even symmetry direct use, in particular to calculate the intermediate odd-numbered row of the first column
class Solution: DEF totalNQueens (Self, n-: int) -> int: Results = [0] DEF BackTrack (half_col, Row = -1, COL = 0,): # back termination condition, if the last line, indicating to find a solution of the stored IF Row. 1-n-== : # solution = [] # for _, in the sorted COL (Queens): # '.' solution.append (COL * + 'Q' * + (n-'.' - COL -. 1)) # results.append (Solution) # Print (Results) Results [0] = Results [0] +. 1 return Row + =. 1 IF (Row == 0): # First Due to the symmetry line, so that when n is an odd number, only half of the traversing column for COL in half_col: IF cols [COL] + P [COL + Row] + Q [COL-Row] == 0: queens.add ((Row, COL)) cols [COL] =. 1 P [COL + Row]. 1 = Q [COL -ROW] =. 1 BackTrack (half_col, Row, COL) queens.remove ((Row, COL)) cols [COL] = 0 P [COL + Row] = 0 Q [COL- Row] = 0 the else : for COL in Range (n-): IF cols [COL] + P [COL + Row] + Q [COL-Row] == 0: queens.add ((Row, COL)) cols [ COL] =. 1 P [COL + Row]. 1 = Q [COL -ROW]. 1 = # transferred to note here special parameters, positional parameters must pass # BackTrack (Row, COL) which is the original writing error backtrack (half_col, row , COL) queens.remove ((Row, COL)) cols [COL] = 0 P [COL + Row] = 0 Q [COL - Row] = 0 cols = [0] * n- P = [0] * (2 * n--. 1 ) Q = [0] * (2 * n--. 1 ) Queens = SET () iF n% 2 == 0: BackTrack (half_col = Range (int (n / 2 ))) return Results [0] * 2 # after the time when n is odd, been traversed half of the column, but also traversing the column intermediate the else : BackTrack (half_col = Range (int (n-/ 2 ))) Results [0]Results = [0] * 2 # Note that n is an odd number, the middle number is int (n / 2) BackTrack (half_col = [int (n / 2 )]) return Results [0] IF the __name__ == ' __main__ ' : duixiang = Solution () WW = duixiang.totalNQueens (. 1 ) Print ( ' result: ' , WW)