classSolution(object): deftotalNQueens(self, n): """ :type n: int :rtype: int """ ans = [0] defdfs(position, ans, idx): for i inrange(0, n): position[idx] = i if checked(position, idx): if idx == n-1: ans[0] += 1 else: dfs(position, ans, idx+1) defchecked(position, idx): for i inrange(idx): if position[i] == position[idx] or idx-i == abs(position[idx] - position[i]): returnFalse returnTrue dfs([0for i inrange(n)], ans, 0) return ans[0]