51 N-Queens

1. Question

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 integern, return all distinct solutions to then-queens puzzle.

Each solution contains a distinct board configuration of then-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.

2. Implementation

(1) Backtracking

思路: 我们对每一行的所有列都尝试摆放queen, 通过三个boolean数组cols, diagnol1, diagnol2分别判断在当前位置的同一列,主对角线和副对角线的位置上是否已经有queen。比较tricky的地方是主对角线和副对角线上怎么判断是否已经有Queen摆放,通过观察我们发现,同一个主对角线上的数的位置都等于 row - col, 为了避免负数出现,我们加上n,即row - col + n, 副对角线上都等于row + col

3. Time & Space Complexity

时间复杂度O(n * 2 ^ n), 每一行里只能放一个queen, 每一列只有放和不放两种可能,所以n * 2 ^ n,空间复杂度O(n)

Last updated

Was this helpful?