The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q'
and '.'
both indicate a queen and an empty space respectively.
Example:
Input: 4 Output: [ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ] Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
其实我们可以看成实际上是求符合某些条件的[1, 2, 3, ... n] 的permutation[LeetCode] 47. Permutations II_Medium tag: DFS, backtracking,而条件就是要Q不在一条斜线上,利用[LeetCode] 系统刷题7_Array & numbers中的一条斜线上的条件来判断,另外有个函数来画puzzle, 会利用到''.join(strList) = 'abcd' if strList = ['a', 'b', 'c', 'd']
时间复杂度 = 方案总数 * 构造方案所需时间
O( n ! * n)
Code:
class Solution: def nQueens(self, n): if n < 1: return [] ans, nums = [], [i for i in range(1, n + 1)] search(ans, [], nums) return ans def search(self, ans, temp, nums): if not nums: ans.append(self.drawPuzzle(temp)) else: for i in range(len(nums)): if self.isValid(temp, nums[i]): self.search(ans, temp + [nums[i]], nums[:i] + nums[i + 1:]) def isValid(self, temp, num): length = len(temp) for r, c in enumerate(temp, 1): if r + c == num + length + 1 or r - c == length + 1 - num: return False return True def drawPuzzle(self, temp): n = len(temp) puzzle = [['.'] * n for _ in range(n)] for r, c in enumerate(temp): puzzle[r][c - 1] = 'Q' for i in len(n): puzzle[i] = ''.join(puzzle[i]) return puzzle