This commit is contained in:
krahets 2023-05-04 05:32:09 +08:00
parent 9b5450e380
commit e519d9b6c2
2 changed files with 65 additions and 7 deletions

View File

@ -148,10 +148,10 @@ comments: true
row: int, row: int,
n: int, n: int,
state: list[list[str]], state: list[list[str]],
res: list[list[list[str]]],
cols: list[bool], cols: list[bool],
diags1: list[bool], diags1: list[bool],
diags2: list[bool], diags2: list[bool],
res: list[list[list[str]]],
): ):
"""回溯算法N 皇后""" """回溯算法N 皇后"""
# 当放置完所有行时,记录解 # 当放置完所有行时,记录解
@ -169,7 +169,7 @@ comments: true
state[row][col] = "Q" state[row][col] = "Q"
cols[col] = diags1[diag1] = diags2[diag2] = True cols[col] = diags1[diag1] = diags2[diag2] = True
# 放置下一行 # 放置下一行
backtrack(row + 1, n, state, cols, diags1, diags2, res) backtrack(row + 1, n, state, res, cols, diags1, diags2)
# 回退:将该格子恢复为空位 # 回退:将该格子恢复为空位
state[row][col] = "#" state[row][col] = "#"
cols[col] = diags1[diag1] = diags2[diag2] = False cols[col] = diags1[diag1] = diags2[diag2] = False
@ -182,7 +182,7 @@ comments: true
diags1 = [False] * (2 * n - 1) # 记录主对角线是否有皇后 diags1 = [False] * (2 * n - 1) # 记录主对角线是否有皇后
diags2 = [False] * (2 * n - 1) # 记录副对角线是否有皇后 diags2 = [False] * (2 * n - 1) # 记录副对角线是否有皇后
res = [] res = []
backtrack(0, n, state, cols, diags1, diags2, res) backtrack(0, n, state, res, cols, diags1, diags2)
return res return res
``` ```

View File

@ -207,9 +207,37 @@ comments: true
=== "Swift" === "Swift"
```swift title="permutations_i.swift" ```swift title="permutations_i.swift"
[class]{}-[func]{backtrack} /* 回溯算法:全排列 I */
func backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {
// 当状态长度等于元素数量时,记录解
if state.count == choices.count {
res.append(state)
return
}
// 遍历所有选择
for (i, choice) in choices.enumerated() {
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
if !selected[i] {
// 尝试:做出选择,更新状态
selected[i] = true
state.append(choice)
// 进行下一轮选择
backtrack(state: &state, choices: choices, selected: &selected, res: &res)
// 回退:撤销选择,恢复到之前的状态
selected[i] = false
state.removeLast()
}
}
}
[class]{}-[func]{permutationsI} /* 全排列 I */
func permutationsI(nums: [Int]) -> [[Int]] {
var state: [Int] = []
var selected = Array(repeating: false, count: nums.count)
var res: [[Int]] = []
backtrack(state: &state, choices: nums, selected: &selected, res: &res)
return res
}
``` ```
=== "Zig" === "Zig"
@ -429,9 +457,39 @@ comments: true
=== "Swift" === "Swift"
```swift title="permutations_ii.swift" ```swift title="permutations_ii.swift"
[class]{}-[func]{backtrack} /* 回溯算法:全排列 II */
func backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {
// 当状态长度等于元素数量时,记录解
if state.count == choices.count {
res.append(state)
return
}
// 遍历所有选择
var duplicated: Set<Int> = []
for (i, choice) in choices.enumerated() {
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
if !selected[i], !duplicated.contains(choice) {
// 尝试:做出选择,更新状态
duplicated.insert(choice) // 记录选择过的元素值
selected[i] = true
state.append(choice)
// 进行下一轮选择
backtrack(state: &state, choices: choices, selected: &selected, res: &res)
// 回退:撤销选择,恢复到之前的状态
selected[i] = false
state.removeLast()
}
}
}
[class]{}-[func]{permutationsII} /* 全排列 II */
func permutationsII(nums: [Int]) -> [[Int]] {
var state: [Int] = []
var selected = Array(repeating: false, count: nums.count)
var res: [[Int]] = []
backtrack(state: &state, choices: nums, selected: &selected, res: &res)
return res
}
``` ```
=== "Zig" === "Zig"