hello-algo/docs/chapter_backtracking/n_queens_problem.md

643 lines
35 KiB
Markdown
Raw Normal View History

2023-10-06 13:31:21 +08:00
---
comments: true
---
2024-01-12 21:13:51 +08:00
# 13.4   n 皇后问题
2023-10-06 13:31:21 +08:00
!!! question
2023-12-02 06:24:05 +08:00
根据国际象棋的规则,皇后可以攻击与同处一行、一列或一条斜线上的棋子。给定 $n$ 个皇后和一个 $n \times n$ 大小的棋盘,寻找使得所有皇后之间无法相互攻击的摆放方案。
2023-10-06 13:31:21 +08:00
如图 13-15 所示,当 $n = 4$ 时,共可以找到两个解。从回溯算法的角度看,$n \times n$ 大小的棋盘共有 $n^2$ 个格子,给出了所有的选择 `choices` 。在逐个放置皇后的过程中,棋盘状态在不断地变化,每个时刻的棋盘就是状态 `state`
2023-11-09 05:13:48 +08:00
![4 皇后问题的解](n_queens_problem.assets/solution_4_queens.png){ class="animation-figure" }
2023-10-06 13:31:21 +08:00
<p align="center"> 图 13-15 &nbsp; 4 皇后问题的解 </p>
2023-12-28 17:18:37 +08:00
图 13-16 展示了本题的三个约束条件:**多个皇后不能在同一行、同一列、同一条对角线上**。值得注意的是,对角线分为主对角线 `\` 和次对角线 `/` 两种。
2023-10-06 13:31:21 +08:00
2023-11-09 05:13:48 +08:00
![n 皇后问题的约束条件](n_queens_problem.assets/n_queens_constraints.png){ class="animation-figure" }
2023-10-06 13:31:21 +08:00
<p align="center"> 图 13-16 &nbsp; n 皇后问题的约束条件 </p>
### 1. &nbsp; 逐行放置策略
皇后的数量和棋盘的行数都为 $n$ ,因此我们容易得到一个推论:**棋盘每行都允许且只允许放置一个皇后**。
也就是说,我们可以采取逐行放置策略:从第一行开始,在每行放置一个皇后,直至最后一行结束。
2023-12-02 06:24:05 +08:00
图 13-17 所示为 $4$ 皇后问题的逐行放置过程。受画幅限制,图 13-17 仅展开了第一行的其中一个搜索分支,并且将不满足列约束和对角线约束的方案都进行了剪枝。
2023-10-06 13:31:21 +08:00
2023-11-09 05:13:48 +08:00
![逐行放置策略](n_queens_problem.assets/n_queens_placing.png){ class="animation-figure" }
2023-10-06 13:31:21 +08:00
<p align="center"> 图 13-17 &nbsp; 逐行放置策略 </p>
2023-12-02 06:24:05 +08:00
从本质上看,**逐行放置策略起到了剪枝的作用**,它避免了同一行出现多个皇后的所有搜索分支。
2023-10-06 13:31:21 +08:00
### 2. &nbsp; 列与对角线剪枝
为了满足列约束,我们可以利用一个长度为 $n$ 的布尔型数组 `cols` 记录每一列是否有皇后。在每次决定放置前,我们通过 `cols` 将已有皇后的列进行剪枝,并在回溯中动态更新 `cols` 的状态。
那么,如何处理对角线约束呢?设棋盘中某个格子的行列索引为 $(row, col)$ ,选定矩阵中的某条主对角线,我们发现该对角线上所有格子的行索引减列索引都相等,**即对角线上所有格子的 $row - col$ 为恒定值**。
2023-12-02 06:24:05 +08:00
也就是说,如果两个格子满足 $row_1 - col_1 = row_2 - col_2$ ,则它们一定处在同一条主对角线上。利用该规律,我们可以借助图 13-18 所示的数组 `diags1` 记录每条主对角线上是否有皇后。
2023-10-06 13:31:21 +08:00
2023-11-03 22:49:42 +08:00
同理,**次对角线上的所有格子的 $row + col$ 是恒定值**。我们同样也可以借助数组 `diags2` 来处理次对角线约束。
2023-10-06 13:31:21 +08:00
2023-11-09 05:13:48 +08:00
![处理列约束和对角线约束](n_queens_problem.assets/n_queens_cols_diagonals.png){ class="animation-figure" }
2023-10-06 13:31:21 +08:00
<p align="center"> 图 13-18 &nbsp; 处理列约束和对角线约束 </p>
### 3. &nbsp; 代码实现
2023-11-03 22:49:42 +08:00
请注意,$n$ 维方阵中 $row - col$ 的范围是 $[-n + 1, n - 1]$ $row + col$ 的范围是 $[0, 2n - 2]$ ,所以主对角线和次对角线的数量都为 $2n - 1$ ,即数组 `diags1``diags2` 的长度都为 $2n - 1$ 。
2023-10-06 13:31:21 +08:00
=== "Python"
```python title="n_queens.py"
2023-10-06 14:10:18 +08:00
def backtrack(
row: int,
n: int,
state: list[list[str]],
res: list[list[list[str]]],
cols: list[bool],
diags1: list[bool],
diags2: list[bool],
):
2024-01-12 21:13:51 +08:00
"""回溯算法n 皇后"""
2023-10-06 14:10:18 +08:00
# 当放置完所有行时,记录解
if row == n:
res.append([list(row) for row in state])
return
# 遍历所有列
for col in range(n):
2023-12-28 17:18:37 +08:00
# 计算该格子对应的主对角线和次对角线
2023-10-06 14:10:18 +08:00
diag1 = row - col + n - 1
diag2 = row + col
2023-12-28 17:18:37 +08:00
# 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后
2023-10-06 14:10:18 +08:00
if not cols[col] and not diags1[diag1] and not diags2[diag2]:
# 尝试:将皇后放置在该格子
state[row][col] = "Q"
cols[col] = diags1[diag1] = diags2[diag2] = True
# 放置下一行
backtrack(row + 1, n, state, res, cols, diags1, diags2)
# 回退:将该格子恢复为空位
state[row][col] = "#"
cols[col] = diags1[diag1] = diags2[diag2] = False
def n_queens(n: int) -> list[list[list[str]]]:
2024-01-12 21:13:51 +08:00
"""求解 n 皇后"""
2023-10-06 14:10:18 +08:00
# 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
state = [["#" for _ in range(n)] for _ in range(n)]
cols = [False] * n # 记录列是否有皇后
2023-12-02 06:24:05 +08:00
diags1 = [False] * (2 * n - 1) # 记录主对角线上是否有皇后
2023-12-28 17:18:37 +08:00
diags2 = [False] * (2 * n - 1) # 记录次对角线上是否有皇后
2023-10-06 14:10:18 +08:00
res = []
backtrack(0, n, state, res, cols, diags1, diags2)
return res
2023-10-06 13:31:21 +08:00
```
=== "C++"
```cpp title="n_queens.cpp"
2024-01-12 21:13:51 +08:00
/* 回溯算法n 皇后 */
2023-10-06 14:10:18 +08:00
void backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vector<string>>> &res, vector<bool> &cols,
vector<bool> &diags1, vector<bool> &diags2) {
// 当放置完所有行时,记录解
if (row == n) {
res.push_back(state);
return;
}
// 遍历所有列
for (int col = 0; col < n; col++) {
2023-12-28 17:18:37 +08:00
// 计算该格子对应的主对角线和次对角线
2023-10-06 14:10:18 +08:00
int diag1 = row - col + n - 1;
int diag2 = row + col;
2023-12-28 17:18:37 +08:00
// 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后
2023-10-06 14:10:18 +08:00
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 尝试:将皇后放置在该格子
state[row][col] = "Q";
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 放置下一行
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 回退:将该格子恢复为空位
state[row][col] = "#";
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
2024-01-12 21:13:51 +08:00
/* 求解 n 皇后 */
2023-10-06 14:10:18 +08:00
vector<vector<vector<string>>> nQueens(int n) {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
vector<vector<string>> state(n, vector<string>(n, "#"));
vector<bool> cols(n, false); // 记录列是否有皇后
2023-12-02 06:24:05 +08:00
vector<bool> diags1(2 * n - 1, false); // 记录主对角线上是否有皇后
2023-12-28 17:18:37 +08:00
vector<bool> diags2(2 * n - 1, false); // 记录次对角线上是否有皇后
2023-10-06 14:10:18 +08:00
vector<vector<vector<string>>> res;
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
2023-10-06 13:31:21 +08:00
```
=== "Java"
```java title="n_queens.java"
2024-01-12 21:13:51 +08:00
/* 回溯算法n 皇后 */
2023-10-06 14:10:18 +08:00
void backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,
boolean[] cols, boolean[] diags1, boolean[] diags2) {
// 当放置完所有行时,记录解
if (row == n) {
List<List<String>> copyState = new ArrayList<>();
for (List<String> sRow : state) {
copyState.add(new ArrayList<>(sRow));
}
res.add(copyState);
return;
}
// 遍历所有列
for (int col = 0; col < n; col++) {
2023-12-28 17:18:37 +08:00
// 计算该格子对应的主对角线和次对角线
2023-10-06 14:10:18 +08:00
int diag1 = row - col + n - 1;
int diag2 = row + col;
2023-12-28 17:18:37 +08:00
// 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后
2023-10-06 14:10:18 +08:00
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 尝试:将皇后放置在该格子
state.get(row).set(col, "Q");
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 放置下一行
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 回退:将该格子恢复为空位
state.get(row).set(col, "#");
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
2024-01-12 21:13:51 +08:00
/* 求解 n 皇后 */
2023-10-06 14:10:18 +08:00
List<List<List<String>>> nQueens(int n) {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
List<List<String>> state = new ArrayList<>();
for (int i = 0; i < n; i++) {
List<String> row = new ArrayList<>();
for (int j = 0; j < n; j++) {
row.add("#");
}
state.add(row);
}
boolean[] cols = new boolean[n]; // 记录列是否有皇后
2023-12-02 06:24:05 +08:00
boolean[] diags1 = new boolean[2 * n - 1]; // 记录主对角线上是否有皇后
2023-12-28 17:18:37 +08:00
boolean[] diags2 = new boolean[2 * n - 1]; // 记录次对角线上是否有皇后
2023-10-06 14:10:18 +08:00
List<List<List<String>>> res = new ArrayList<>();
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
2023-10-06 13:31:21 +08:00
```
=== "C#"
```csharp title="n_queens.cs"
2024-01-12 21:13:51 +08:00
/* 回溯算法n 皇后 */
2023-10-08 01:43:28 +08:00
void Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,
2023-10-06 14:10:18 +08:00
bool[] cols, bool[] diags1, bool[] diags2) {
// 当放置完所有行时,记录解
if (row == n) {
2023-11-27 02:32:06 +08:00
List<List<string>> copyState = [];
2023-10-06 14:10:18 +08:00
foreach (List<string> sRow in state) {
copyState.Add(new List<string>(sRow));
}
res.Add(copyState);
return;
}
// 遍历所有列
for (int col = 0; col < n; col++) {
2023-12-28 17:18:37 +08:00
// 计算该格子对应的主对角线和次对角线
2023-10-06 14:10:18 +08:00
int diag1 = row - col + n - 1;
int diag2 = row + col;
2023-12-28 17:18:37 +08:00
// 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后
2023-10-06 14:10:18 +08:00
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 尝试:将皇后放置在该格子
state[row][col] = "Q";
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 放置下一行
2023-10-08 01:43:28 +08:00
Backtrack(row + 1, n, state, res, cols, diags1, diags2);
2023-10-06 14:10:18 +08:00
// 回退:将该格子恢复为空位
state[row][col] = "#";
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
2024-01-12 21:13:51 +08:00
/* 求解 n 皇后 */
2023-10-08 01:43:28 +08:00
List<List<List<string>>> NQueens(int n) {
2023-10-06 14:10:18 +08:00
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
2023-11-27 02:32:06 +08:00
List<List<string>> state = [];
2023-10-06 14:10:18 +08:00
for (int i = 0; i < n; i++) {
2023-11-27 02:32:06 +08:00
List<string> row = [];
2023-10-06 14:10:18 +08:00
for (int j = 0; j < n; j++) {
row.Add("#");
}
state.Add(row);
}
bool[] cols = new bool[n]; // 记录列是否有皇后
2023-12-02 06:24:05 +08:00
bool[] diags1 = new bool[2 * n - 1]; // 记录主对角线上是否有皇后
2023-12-28 17:18:37 +08:00
bool[] diags2 = new bool[2 * n - 1]; // 记录次对角线上是否有皇后
2023-11-27 02:32:06 +08:00
List<List<List<string>>> res = [];
2023-10-06 14:10:18 +08:00
2023-10-08 01:43:28 +08:00
Backtrack(0, n, state, res, cols, diags1, diags2);
2023-10-06 14:10:18 +08:00
return res;
}
2023-10-06 13:31:21 +08:00
```
=== "Go"
```go title="n_queens.go"
2024-01-12 21:13:51 +08:00
/* 回溯算法n 皇后 */
2023-10-06 14:10:18 +08:00
func backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {
// 当放置完所有行时,记录解
if row == n {
newState := make([][]string, len(*state))
for i, _ := range newState {
newState[i] = make([]string, len((*state)[0]))
copy(newState[i], (*state)[i])
}
*res = append(*res, newState)
}
// 遍历所有列
for col := 0; col < n; col++ {
2023-12-28 17:18:37 +08:00
// 计算该格子对应的主对角线和次对角线
2023-10-06 14:10:18 +08:00
diag1 := row - col + n - 1
diag2 := row + col
2023-12-28 17:18:37 +08:00
// 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后
2023-10-06 14:10:18 +08:00
if !(*cols)[col] && !(*diags1)[diag1] && !(*diags2)[diag2] {
// 尝试:将皇后放置在该格子
(*state)[row][col] = "Q"
(*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = true, true, true
// 放置下一行
backtrack(row+1, n, state, res, cols, diags1, diags2)
// 回退:将该格子恢复为空位
(*state)[row][col] = "#"
(*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = false, false, false
}
}
}
2024-01-12 21:13:51 +08:00
/* 求解 n 皇后 */
2023-10-06 14:10:18 +08:00
func nQueens(n int) [][][]string {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
state := make([][]string, n)
for i := 0; i < n; i++ {
row := make([]string, n)
for i := 0; i < n; i++ {
row[i] = "#"
}
state[i] = row
}
// 记录列是否有皇后
cols := make([]bool, n)
diags1 := make([]bool, 2*n-1)
diags2 := make([]bool, 2*n-1)
res := make([][][]string, 0)
backtrack(0, n, &state, &res, &cols, &diags1, &diags2)
return res
}
2023-10-06 13:31:21 +08:00
```
=== "Swift"
```swift title="n_queens.swift"
2024-01-12 21:13:51 +08:00
/* 回溯算法n 皇后 */
2023-10-06 14:10:18 +08:00
func backtrack(row: Int, n: Int, state: inout [[String]], res: inout [[[String]]], cols: inout [Bool], diags1: inout [Bool], diags2: inout [Bool]) {
// 当放置完所有行时,记录解
if row == n {
res.append(state)
return
}
// 遍历所有列
for col in 0 ..< n {
2023-12-28 17:18:37 +08:00
// 计算该格子对应的主对角线和次对角线
2023-10-06 14:10:18 +08:00
let diag1 = row - col + n - 1
let diag2 = row + col
2023-12-28 17:18:37 +08:00
// 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后
2023-10-06 14:10:18 +08:00
if !cols[col] && !diags1[diag1] && !diags2[diag2] {
// 尝试:将皇后放置在该格子
state[row][col] = "Q"
cols[col] = true
diags1[diag1] = true
diags2[diag2] = true
// 放置下一行
backtrack(row: row + 1, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)
// 回退:将该格子恢复为空位
state[row][col] = "#"
cols[col] = false
diags1[diag1] = false
diags2[diag2] = false
}
}
}
2024-01-12 21:13:51 +08:00
/* 求解 n 皇后 */
2023-10-06 14:10:18 +08:00
func nQueens(n: Int) -> [[[String]]] {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
var state = Array(repeating: Array(repeating: "#", count: n), count: n)
var cols = Array(repeating: false, count: n) // 记录列是否有皇后
2023-12-02 06:24:05 +08:00
var diags1 = Array(repeating: false, count: 2 * n - 1) // 记录主对角线上是否有皇后
2023-12-28 17:18:37 +08:00
var diags2 = Array(repeating: false, count: 2 * n - 1) // 记录次对角线上是否有皇后
2023-10-06 14:10:18 +08:00
var res: [[[String]]] = []
backtrack(row: 0, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)
return res
}
2023-10-06 13:31:21 +08:00
```
=== "JS"
```javascript title="n_queens.js"
2024-01-12 21:13:51 +08:00
/* 回溯算法n 皇后 */
2023-10-06 14:10:18 +08:00
function backtrack(row, n, state, res, cols, diags1, diags2) {
// 当放置完所有行时,记录解
if (row === n) {
res.push(state.map((row) => row.slice()));
return;
}
// 遍历所有列
for (let col = 0; col < n; col++) {
2023-12-28 17:18:37 +08:00
// 计算该格子对应的主对角线和次对角线
2023-10-06 14:10:18 +08:00
const diag1 = row - col + n - 1;
const diag2 = row + col;
2023-12-28 17:18:37 +08:00
// 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后
2023-10-06 14:10:18 +08:00
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 尝试:将皇后放置在该格子
state[row][col] = 'Q';
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 放置下一行
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 回退:将该格子恢复为空位
state[row][col] = '#';
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
2024-01-12 21:13:51 +08:00
/* 求解 n 皇后 */
2023-10-06 14:10:18 +08:00
function nQueens(n) {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
const state = Array.from({ length: n }, () => Array(n).fill('#'));
const cols = Array(n).fill(false); // 记录列是否有皇后
2023-12-02 06:24:05 +08:00
const diags1 = Array(2 * n - 1).fill(false); // 记录主对角线上是否有皇后
2023-12-28 17:18:37 +08:00
const diags2 = Array(2 * n - 1).fill(false); // 记录次对角线上是否有皇后
2023-10-06 14:10:18 +08:00
const res = [];
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
2023-10-06 13:31:21 +08:00
```
=== "TS"
```typescript title="n_queens.ts"
2024-01-12 21:13:51 +08:00
/* 回溯算法n 皇后 */
2023-10-06 14:10:18 +08:00
function backtrack(
row: number,
n: number,
state: string[][],
res: string[][][],
cols: boolean[],
diags1: boolean[],
diags2: boolean[]
): void {
// 当放置完所有行时,记录解
if (row === n) {
res.push(state.map((row) => row.slice()));
return;
}
// 遍历所有列
for (let col = 0; col < n; col++) {
2023-12-28 17:18:37 +08:00
// 计算该格子对应的主对角线和次对角线
2023-10-06 14:10:18 +08:00
const diag1 = row - col + n - 1;
const diag2 = row + col;
2023-12-28 17:18:37 +08:00
// 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后
2023-10-06 14:10:18 +08:00
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 尝试:将皇后放置在该格子
state[row][col] = 'Q';
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 放置下一行
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 回退:将该格子恢复为空位
state[row][col] = '#';
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
2024-01-12 21:13:51 +08:00
/* 求解 n 皇后 */
2023-10-06 14:10:18 +08:00
function nQueens(n: number): string[][][] {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
const state = Array.from({ length: n }, () => Array(n).fill('#'));
const cols = Array(n).fill(false); // 记录列是否有皇后
2023-12-02 06:24:05 +08:00
const diags1 = Array(2 * n - 1).fill(false); // 记录主对角线上是否有皇后
2023-12-28 17:18:37 +08:00
const diags2 = Array(2 * n - 1).fill(false); // 记录次对角线上是否有皇后
2023-10-06 14:10:18 +08:00
const res: string[][][] = [];
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
2023-10-06 13:31:21 +08:00
```
=== "Dart"
```dart title="n_queens.dart"
2024-01-12 21:13:51 +08:00
/* 回溯算法n 皇后 */
2023-10-06 14:10:18 +08:00
void backtrack(
int row,
int n,
List<List<String>> state,
List<List<List<String>>> res,
List<bool> cols,
List<bool> diags1,
List<bool> diags2,
) {
// 当放置完所有行时,记录解
if (row == n) {
List<List<String>> copyState = [];
for (List<String> sRow in state) {
copyState.add(List.from(sRow));
}
res.add(copyState);
return;
}
// 遍历所有列
for (int col = 0; col < n; col++) {
2023-12-28 17:18:37 +08:00
// 计算该格子对应的主对角线和次对角线
2023-10-06 14:10:18 +08:00
int diag1 = row - col + n - 1;
int diag2 = row + col;
2023-12-28 17:18:37 +08:00
// 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后
2023-10-06 14:10:18 +08:00
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 尝试:将皇后放置在该格子
state[row][col] = "Q";
cols[col] = true;
diags1[diag1] = true;
diags2[diag2] = true;
// 放置下一行
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 回退:将该格子恢复为空位
state[row][col] = "#";
cols[col] = false;
diags1[diag1] = false;
diags2[diag2] = false;
}
}
}
2024-01-12 21:13:51 +08:00
/* 求解 n 皇后 */
2023-10-06 14:10:18 +08:00
List<List<List<String>>> nQueens(int n) {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
List<List<String>> state = List.generate(n, (index) => List.filled(n, "#"));
List<bool> cols = List.filled(n, false); // 记录列是否有皇后
2023-12-02 06:24:05 +08:00
List<bool> diags1 = List.filled(2 * n - 1, false); // 记录主对角线上是否有皇后
2023-12-28 17:18:37 +08:00
List<bool> diags2 = List.filled(2 * n - 1, false); // 记录次对角线上是否有皇后
2023-10-06 14:10:18 +08:00
List<List<List<String>>> res = [];
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
2023-10-06 13:31:21 +08:00
```
=== "Rust"
```rust title="n_queens.rs"
2024-01-12 21:13:51 +08:00
/* 回溯算法n 皇后 */
2023-10-06 14:10:18 +08:00
fn backtrack(row: usize, n: usize, state: &mut Vec<Vec<String>>, res: &mut Vec<Vec<Vec<String>>>,
cols: &mut [bool], diags1: &mut [bool], diags2: &mut [bool]) {
// 当放置完所有行时,记录解
if row == n {
let mut copy_state: Vec<Vec<String>> = Vec::new();
for s_row in state.clone() {
copy_state.push(s_row);
}
res.push(copy_state);
return;
}
// 遍历所有列
for col in 0..n {
2023-12-28 17:18:37 +08:00
// 计算该格子对应的主对角线和次对角线
2023-10-06 14:10:18 +08:00
let diag1 = row + n - 1 - col;
let diag2 = row + col;
2023-12-28 17:18:37 +08:00
// 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后
2023-10-06 14:10:18 +08:00
if !cols[col] && !diags1[diag1] && !diags2[diag2] {
// 尝试:将皇后放置在该格子
state.get_mut(row).unwrap()[col] = "Q".into();
(cols[col], diags1[diag1], diags2[diag2]) = (true, true, true);
// 放置下一行
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 回退:将该格子恢复为空位
state.get_mut(row).unwrap()[col] = "#".into();
(cols[col], diags1[diag1], diags2[diag2]) = (false, false, false);
}
}
}
2024-01-12 21:13:51 +08:00
/* 求解 n 皇后 */
2023-10-06 14:10:18 +08:00
fn n_queens(n: usize) -> Vec<Vec<Vec<String>>> {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
let mut state: Vec<Vec<String>> = Vec::new();
for _ in 0..n {
let mut row: Vec<String> = Vec::new();
for _ in 0..n {
row.push("#".into());
}
state.push(row);
}
let mut cols = vec![false; n]; // 记录列是否有皇后
2023-12-02 06:24:05 +08:00
let mut diags1 = vec![false; 2 * n - 1]; // 记录主对角线上是否有皇后
2023-12-28 17:18:37 +08:00
let mut diags2 = vec![false; 2 * n - 1]; // 记录次对角线上是否有皇后
2023-10-06 14:10:18 +08:00
let mut res: Vec<Vec<Vec<String>>> = Vec::new();
backtrack(0, n, &mut state, &mut res, &mut cols, &mut diags1, &mut diags2);
res
}
2023-10-06 13:31:21 +08:00
```
=== "C"
```c title="n_queens.c"
2024-01-12 21:13:51 +08:00
/* 回溯算法n 皇后 */
2023-10-27 20:41:59 +08:00
void backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int *resSize, bool cols[MAX_SIZE],
bool diags1[2 * MAX_SIZE - 1], bool diags2[2 * MAX_SIZE - 1]) {
2023-10-09 05:05:19 +08:00
// 当放置完所有行时,记录解
if (row == n) {
2023-10-18 02:16:55 +08:00
res[*resSize] = (char **)malloc(sizeof(char *) * n);
2023-10-09 05:05:19 +08:00
for (int i = 0; i < n; ++i) {
2023-10-18 02:16:55 +08:00
res[*resSize][i] = (char *)malloc(sizeof(char) * (n + 1));
strcpy(res[*resSize][i], state[i]);
2023-10-09 05:05:19 +08:00
}
2023-10-18 02:16:55 +08:00
(*resSize)++;
2023-10-09 05:05:19 +08:00
return;
}
// 遍历所有列
for (int col = 0; col < n; col++) {
2023-12-28 17:18:37 +08:00
// 计算该格子对应的主对角线和次对角线
2023-10-09 05:05:19 +08:00
int diag1 = row - col + n - 1;
int diag2 = row + col;
2023-12-28 17:18:37 +08:00
// 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后
2023-10-09 05:05:19 +08:00
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 尝试:将皇后放置在该格子
state[row][col] = 'Q';
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 放置下一行
2023-10-18 02:16:55 +08:00
backtrack(row + 1, n, state, res, resSize, cols, diags1, diags2);
2023-10-09 05:05:19 +08:00
// 回退:将该格子恢复为空位
state[row][col] = '#';
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
2024-01-12 21:13:51 +08:00
/* 求解 n 皇后 */
2023-10-18 02:16:55 +08:00
char ***nQueens(int n, int *returnSize) {
2023-10-27 20:41:59 +08:00
char state[MAX_SIZE][MAX_SIZE];
2023-10-09 05:05:19 +08:00
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
state[i][j] = '#';
}
state[i][n] = '\0';
}
2023-10-27 20:41:59 +08:00
bool cols[MAX_SIZE] = {false}; // 记录列是否有皇后
2023-12-02 06:24:05 +08:00
bool diags1[2 * MAX_SIZE - 1] = {false}; // 记录主对角线上是否有皇后
2023-12-28 17:18:37 +08:00
bool diags2[2 * MAX_SIZE - 1] = {false}; // 记录次对角线上是否有皇后
2023-10-09 05:05:19 +08:00
2023-10-27 20:41:59 +08:00
char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);
2023-10-18 02:16:55 +08:00
*returnSize = 0;
backtrack(0, n, state, res, returnSize, cols, diags1, diags2);
2023-10-09 05:05:19 +08:00
return res;
}
2023-10-06 13:31:21 +08:00
```
=== "Zig"
```zig title="n_queens.zig"
[class]{}-[func]{backtrack}
[class]{}-[func]{nQueens}
```
2024-01-07 03:26:23 +08:00
??? pythontutor "可视化运行"
2024-01-09 16:00:24 +08:00
<div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20backtrack%28%0A%20%20%20%20row%3A%20int,%0A%20%20%20%20n%3A%20int,%0A%20%20%20%20state%3A%20list%5Blist%5Bstr%5D%5D,%0A%20%20%20%20res%3A%20list%5Blist%5Blist%5Bstr%5D%5D%5D,%0A%20%20%20%20cols%3A%20list%5Bbool%5D,%0A%20%20%20%20diags1%3A%20list%5Bbool%5D,%0A%20%20%20%20diags2%3A%20list%5Bbool%5D,%0A%29%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%E7%AE%97%E6%B3%95%EF%BC%9AN%20%E7%9A%87%E5%90%8E%22%22%22%0A%20%20%20%20%23%20%E5%BD%93%E6%94%BE%E7%BD%AE%E5%AE%8C%E6%89%80%E6%9C%89%E8%A1%8C%E6%97%B6%EF%BC%8C%E8%AE%B0%E5%BD%95%E8%A7%A3%0A%20%20%20%20if%20row%20%3D%3D%20n%3A%0A%20%20%20%20%20%20%20%20res.append%28%5Blist%28row%29%20for%20row%20in%20state%5D%29%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E6%89%80%E6%9C%89%E5%88%97%0A%20%20%20%20for%20col%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E8%AE%A1%E7%AE%97%E8%AF%A5%E6%A0%BC%E5%AD%90%E5%AF%B9%E5%BA%94%E7%9A%84%E4%B8%BB%E5%AF%B9%E8%A7%92%E7%BA%BF%E5%92%8C%E6%AC%A1%E5%AF%B9%E8%A7%92%E7%BA%BF%0A%20%20%20%20%20%20%20%20diag1%20%3D%20row%20-%20col%20%2B%20n%20-%201%0A%20%20%20%20%20%20%20%20diag2%20%3D%20row%20%2B%20col%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%EF%BC%9A%E4%B8%8D%E5%85%81%E8%AE%B8%E8%AF%A5%E6%A0%BC%E5%AD%90%E6%89%80%E5%9C%A8%E5%88%97%E3%80%81%E4%B8%BB%E5%AF%B9%E8%A7%92%E7%BA%BF%E3%80%81%E6%AC%A1%E5%AF%B9%E8%A7%92%E7%BA%BF%E4%B8%8A%E5%AD%98%E5%9C%A8%E7%9A%87%E5%90%8E%0A%20%20%20%20%20%20%20%20if%20not%20cols%5Bcol%5D%20and%20not%20diags1%5Bdiag1%5D%20and%20not%20diags2%5Bdiag2%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%B0%9D%E8%AF%95%EF%BC%9A%E5%B0%86%E7%9A%87%E5%90%8E%E6%94%BE%E7%BD%AE%E5%9C%A8%E8%AF%A5%E6%A0%BC%E5%AD%90%0A%20%20%20%20%20%20%20%20%20%20%20%20state%5Brow%5D%5Bcol%5D%20%3D%20%22Q%22%0A%20%20%20%20%20%20%20%20%20%20%20%20cols%5Bcol%5D%20%3D%20diags1%5Bdiag1%5D%20%3D%20diags2%5Bdiag2%5D%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E6%94%BE%E7%BD%AE%E4%B8%8B%E4%B8%80%E8%A1%8C%0A%20%20%20%20%20%20%20%20%20%20%20%20backtrack%28row%20%2B%201,%20n,%20state,%20res,%20cols,%20diags1,%20diags2%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%EF%BC%9A%E5%B0%86%E8%AF%A5%E6%A0%BC%E5%AD%90%E6%81%A2%E5%A4%8D%E4%B8%BA%E7%A9%BA%E4%BD%8D%0A%20%20%20%20%20%20%20%20%20%20%20%20state%5Brow%5D%5Bcol%5D%20%3D%20%22%23%22%0A%20%20%20%20%20%20%20%20%20%20%20%20cols%5Bcol%5D%20%3D%20diags1%5Bdiag1%5D%20%3D%20diags2%5Bdiag2%5D%20%3D%20False%0A%0A%0Adef%20n_queens%28n%3A%20int%29%20-%3E%20list%5Blist%5Blist%5Bstr%5D%5D%5D%3A%0A%20%20%20%20%22%22%22%E6%B1%82%E8%A7%A3%20N%20%E7%9A%87%E5%90%8E%22%22%22%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%20n*n%20%E5%A4%A7%E5%B0%8F%E7%9A%84%E6%A3%8B%E7%9B%98%EF%BC%8C%E5%85%B6%E4%B8%AD%20'Q'%20%E4%BB%A3%E8%A1%A8%E7%9A%87%E5%90%8E%EF%BC%8C'%23'%20%E4%BB%A3%E8%A1%A8%E7%A9%BA%E4%BD%8D%0A%20%20%20%20state%20%3D%20%5B%5B%22%23%22%20for%20_%20in%20range%28n%29%5D%20for%20_%20in%20range%28n%29%5D%0A%20%20%20%20cols%20%3D%20%5BFalse%5D%20*%20n%20%20%23%20%E8%AE%B0%E5%BD%95%E5%88%97%E6%98%AF%E5%90%A6%E6%9C%89%E7%9A%87%E5%90%8E%0A%20%20%20%20diags1%20%3D%20%5BFalse%5D%20*%20%282%20*%20n%20-%201%29%20%20%23%20%E8%AE%B0%E5%BD%95%E4%B8%BB%E5%AF%B9%E8%A7%92%E7%BA%BF%E4%B8%8A%E6%98%AF%E5%90%A6%E6%9C%89%E7%9A%87%E5%90%8E%0A%20%20%20%20diags2%20%3D%20%5BFalse%5D%20*%20%282%20*%20n%20-%201%29%20%20%23%20%E8%AE%B0%E5%BD%95%E6%AC%A1%E5%AF%B9%E8%A7%92%E7%BA%BF%E4%B8%8A%E6%98%AF%E5%90%A6%E6%9C%89%E7%9A%87%E5%90%8E%0A%20%20%20%20res%20%3D%20%5B%5D%0A%20%20%20%20backtrack%280,%20n,%20state,%20res,%20cols,%20diags1,%20diags2%29%0A%0A%20%20%20%20return%20res%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%204%0A%20%20%20%20res%20%3D%20n_queens%28n%29%0A%0A%20%20%20%20print%28f%22%E8%BE%93%E5%85%A5%E6%A3%8B%E7%9B%98%E9%95%BF%E5%AE%BD%E4%B8%BA%20%7Bn%7D%22%29%0A%20%20%20%20print%28f%22%E7%9A%87%E5%90%8E%E6%94%BE%E7%BD%AE%E6%96%B9%E6%A1%88%E5%85%B1%E6%9C%89%20%7Blen%28res%29%7D
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20backtrack%28%0A%20%20%20%20row%3A%20int,%0A%20%20%20%20n%3A%20int,%0A%20%20%20%20state%3A%20list%5Blist%5Bstr%5D%5D,%0A%20%20%20%20res%3A%20list%5Blist%5Blist%5Bstr%5D%5D%5D,%0A%20%20%20%20cols%3A%20list%5Bbool%5D,%0A%20%20%20%20diags1%3A%20list%5Bbool%5D,%0A%20%20%20%20diags2%3A%20list%5Bbool%5D,%0A%29%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%E7%AE%97%E6%B3%95%EF%BC%9AN%20%E7%9A%87%E5%90%8E%22%22%22%0A%20%20%20%20%23%20%E5%BD%93%E6%94%BE%E7%BD%AE%E5%AE%8C%E6%89%80%E6%9C%89%E8%A1%8C%E6%97%B6%EF%BC%8C%E8%AE%B0%E5%BD%95%E8%A7%A3%0A%20%20%20%20if%20row%20%3D%3D%20n%3A%0A%20%20%20%20%20%20%20%20res.append%28%5Blist%28row%29%20for%20row%20in%20state%5D%29%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E6%89%80%E6%9C%89%E5%88%97%0A%20%20%20%20for%20col%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E8%AE%A1%E7%AE%97%E8%AF%A5%E6%A0%BC%E5%AD%90%E5%AF%B9%E5%BA%94%E7%9A%84%E4%B8%BB%E5%AF%B9%E8%A7%92%E7%BA%BF%E5%92%8C%E6%AC%A1%E5%AF%B9%E8%A7%92%E7%BA%BF%0A%20%20%20%20%20%20%20%20diag1%20%3D%20row%20-%20col%20%2B%20n%20-%201%0A%20%20%20%20%20%20%20%20diag2%20%3D%20row%20%2B%20col%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%EF%BC%9A%E4%B8%8D%E5%85%81%E8%AE%B8%E8%AF%A5%E6%A0%BC%E5%AD%90%E6%89%80%E5%9C%A8%E5%88%97%E3%80%81%E4%B8%BB%E5%AF%B9%E8%A7%92%E7%BA%BF%E3%80%81%E6%AC%A1%E5%AF%B9%E8%A7%92%E7%BA%BF%E4%B8%8A%E5%AD%98%E5%9C%A8%E7%9A%87%E5%90%8E%0A%20%20%20%20%20%20%20%20if%20not%20cols%5Bcol%5D%20and%20not%20diags1%5Bdiag1%5D%20and%20not%20diags2%5Bdiag2%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%B0%9D%E8%AF%95%EF%BC%9A%E5%B0%86%E7%9A%87%E5%90%8E%E6%94%BE%E7%BD%AE%E5%9C%A8%E8%AF%A5%E6%A0%BC%E5%AD%90%0A%20%20%20%20%20%20%20%20%20%20%20%20state%5Brow%5D%5Bcol%5D%20%3D%20%22Q%22%0A%20%20%20%20%20%20%20%20%20%20%20%20cols%5Bcol%5D%20%3D%20diags1%5Bdiag1%5D%20%3D%20diags2%5Bdiag2%5D%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E6%94%BE%E7%BD%AE%E4%B8%8B%E4%B8%80%E8%A1%8C%0A%20%20%20%20%20%20%20%20%20%20%20%20backtrack%28row%20%2B%201,%20n,%20state,%20res,%20cols,%20diags1,%20diags2%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%EF%BC%9A%E5%B0%86%E8%AF%A5%E6%A0%BC%E5%AD%90%E6%81%A2%E5%A4%8D%E4%B8%BA%E7%A9%BA%E4%BD%8D%0A%20%20%20%20%20%20%20%20%20%20%20%20state%5Brow%5D%5Bcol%5D%20%3D%20%22%23%22%0A%20%20%20%20%20%20%20%20%20%20%20%20cols%5Bcol%5D%20%3D%20diags1%5Bdiag1%5D%20%3D%20diags2%5Bdiag2%5D%20%3D%20False%0A%0A%0Adef%20n_queens%28n%3A%20int%29%20-%3E%20list%5Blist%5Blist%5Bstr%5D%5D%5D%3A%0A%20%20%20%20%22%22%22%E6%B1%82%E8%A7%A3%20N%20%E7%9A%87%E5%90%8E%22%22%22%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%20n*n%20%E5%A4%A7%E5%B0%8F%E7%9A%84%E6%A3%8B%E7%9B%98%EF%BC%8C%E5%85%B6%E4%B8%AD%20'Q'%20%E4%BB%A3%E8%A1%A8%E7%9A%87%E5%90%8E%EF%BC%8C'%23'%20%E4%BB%A3%E8%A1%A8%E7%A9%BA%E4%BD%8D%0A%20%20%20%20state%20%3D%20%5B%5B%22%23%22%20for%20_%20in%20range%28n%29%5D%20for%20_%20in%20range%28n%29%5D%0A%20%20%20%20cols%20%3D%20%5BFalse%5D%20*%20n%20%20%23%20%E8%AE%B0%E5%BD%95%E5%88%97%E6%98%AF%E5%90%A6%E6%9C%89%E7%9A%87%E5%90%8E%0A%20%20%20%20diags1%20%3D%20%5BFalse%5D%20*%20%282%20*%20n%20-%201%29%20%20%23%20%E8%AE%B0%E5%BD%95%E4%B8%BB%E5%AF%B9%E8%A7%92%E7%BA%BF%E4%B8%8A%E6%98%AF%E5%90%A6%E6%9C%89%E7%9A%87%E5%90%8E%0A%20%20%20%20diags2%20%3D%20%5BFalse%5D%20*%20%282%20*%20n%20-%201%29%20%20%23%20%E8%AE%B0%E5%BD%95%E6%AC%A1%E5%AF%B9%E8%A7%92%E7%BA%BF%E4%B8%8A%E6%98%AF%E5%90%A6%E6%9C%89%E7%9A%87%E5%90%8E%0A%20%20%20%20res%20%3D%20%5B%5D%0A%20%20%20%20backtrack%280,%20n,%20state,%20res,%20cols,%20diags1,%20diags2%29%0A%0A%20%20%20%20return%20res%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%204%0A%20%20%20%20res%20%3D%20n_queens%28n%29%0A%0A%20%20%20%20print%28f%22%E8%BE%93%E5%85%A5%E6%A3%8B%E7%9B%98%E9%95%BF%E5%AE%BD%E4%B8%BA%20%7Bn%7D%22%29%0A%20%20%20%20print%28f%22%E7%9A%87%E5%90%8E%E6%94%BE%E7%BD%AE%E6%96%B9%E6%A1%88%E5%85%B1%E6%9C%89%20%7Blen%28res%29%7D%20%E7%A7%8D%22%29%0A%20%20%20%20for%20sta
2024-01-07 03:26:23 +08:00
2024-02-17 06:16:00 +08:00
逐行放置 $n$ 次,考虑列约束,则从第一行到最后一行分别有 $n$、$n-1$、$\dots$、$2$、$1$ 个选择,使用 $O(n!)$ 时间。当记录解时,需要复制矩阵 `state` 并添加进 `res` ,复制操作使用 $O(n^2)$ 时间。因此,**总体时间复杂度为 $O(n! \cdot n^2)$** 。实际上,根据对角线约束的剪枝也能够大幅缩小搜索空间,因而搜索效率往往优于以上时间复杂度。
2023-10-06 13:31:21 +08:00
数组 `state` 使用 $O(n^2)$ 空间,数组 `cols`、`diags1` 和 `diags2` 皆使用 $O(n)$ 空间。最大递归深度为 $n$ ,使用 $O(n)$ 栈帧空间。因此,**空间复杂度为 $O(n^2)$** 。