This commit is contained in:
krahets 2023-05-03 18:29:57 +08:00
parent 4eb7734fa9
commit 7645bf97df
3 changed files with 20 additions and 2 deletions

View file

@ -502,6 +502,7 @@ def backtrack(state, choices, res):
if (isValid(state, choice)) { if (isValid(state, choice)) {
// 尝试:做出选择,更新状态 // 尝试:做出选择,更新状态
makeChoice(state, choice); makeChoice(state, choice);
// 进行下一轮选择
backtrack(state, Arrays.asList(choice.left, choice.right), res); backtrack(state, Arrays.asList(choice.left, choice.right), res);
// 回退:撤销选择,恢复到之前的状态 // 回退:撤销选择,恢复到之前的状态
undoChoice(state, choice); undoChoice(state, choice);
@ -552,6 +553,7 @@ def backtrack(state, choices, res):
if (isValid(state, choice)) { if (isValid(state, choice)) {
// 尝试:做出选择,更新状态 // 尝试:做出选择,更新状态
makeChoice(state, choice); makeChoice(state, choice);
// 进行下一轮选择
vector<TreeNode *> nextChoices{choice->left, choice->right}; vector<TreeNode *> nextChoices{choice->left, choice->right};
backtrack(state, nextChoices, res); backtrack(state, nextChoices, res);
// 回退:撤销选择,恢复到之前的状态 // 回退:撤销选择,恢复到之前的状态
@ -597,6 +599,7 @@ def backtrack(state, choices, res):
if is_valid(state, choice): if is_valid(state, choice):
# 尝试:做出选择,更新状态 # 尝试:做出选择,更新状态
make_choice(state, choice) make_choice(state, choice)
# 进行下一轮选择
backtrack(state, [choice.left, choice.right], res) backtrack(state, [choice.left, choice.right], res)
# 回退:撤销选择,恢复到之前的状态 # 回退:撤销选择,恢复到之前的状态
undo_choice(state, choice) undo_choice(state, choice)
@ -708,8 +711,8 @@ def backtrack(state, choices, res):
if (isValid(state, choice)) { if (isValid(state, choice)) {
// 尝试:做出选择,更新状态 // 尝试:做出选择,更新状态
makeChoice(state, choice); makeChoice(state, choice);
List<TreeNode> nextChoices = new List<TreeNode>() { choice.left, choice.right }; // 进行下一轮选择
backtrack(state, nextChoices, res); backtrack(state, new List<TreeNode> { choice.left, choice.right }, res);
// 回退:撤销选择,恢复到之前的状态 // 回退:撤销选择,恢复到之前的状态
undoChoice(state, choice); undoChoice(state, choice);
} }

View file

@ -52,6 +52,7 @@ comments: true
// 尝试:做出选择,更新状态 // 尝试:做出选择,更新状态
selected[i] = true; selected[i] = true;
state.add(choice); state.add(choice);
// 进行下一轮选择
backtrack(state, choices, selected, res); backtrack(state, choices, selected, res);
// 回退:撤销选择,恢复到之前的状态 // 回退:撤销选择,恢复到之前的状态
selected[i] = false; selected[i] = false;
@ -86,6 +87,7 @@ comments: true
// 尝试:做出选择,更新状态 // 尝试:做出选择,更新状态
selected[i] = true; selected[i] = true;
state.push_back(choice); state.push_back(choice);
// 进行下一轮选择
backtrack(state, choices, selected, res); backtrack(state, choices, selected, res);
// 回退:撤销选择,恢复到之前的状态 // 回退:撤销选择,恢复到之前的状态
selected[i] = false; selected[i] = false;
@ -122,6 +124,7 @@ comments: true
# 尝试:做出选择,更新状态 # 尝试:做出选择,更新状态
selected[i] = True selected[i] = True
state.append(choice) state.append(choice)
# 进行下一轮选择
backtrack(state, choices, selected, res) backtrack(state, choices, selected, res)
# 回退:撤销选择,恢复到之前的状态 # 回退:撤销选择,恢复到之前的状态
selected[i] = False selected[i] = False
@ -184,6 +187,7 @@ comments: true
// 尝试:做出选择,更新状态 // 尝试:做出选择,更新状态
selected[i] = true; selected[i] = true;
state.Add(choice); state.Add(choice);
// 进行下一轮选择
backtrack(state, choices, selected, res); backtrack(state, choices, selected, res);
// 回退:撤销选择,恢复到之前的状态 // 回退:撤销选择,恢复到之前的状态
selected[i] = false; selected[i] = false;
@ -264,6 +268,7 @@ comments: true
duplicated.add(choice); // 记录选择过的元素值 duplicated.add(choice); // 记录选择过的元素值
selected[i] = true; selected[i] = true;
state.add(choice); state.add(choice);
// 进行下一轮选择
backtrack(state, choices, selected, res); backtrack(state, choices, selected, res);
// 回退:撤销选择,恢复到之前的状态 // 回退:撤销选择,恢复到之前的状态
selected[i] = false; selected[i] = false;
@ -300,6 +305,7 @@ comments: true
duplicated.emplace(choice); // 记录选择过的元素值 duplicated.emplace(choice); // 记录选择过的元素值
selected[i] = true; selected[i] = true;
state.push_back(choice); state.push_back(choice);
// 进行下一轮选择
backtrack(state, choices, selected, res); backtrack(state, choices, selected, res);
// 回退:撤销选择,恢复到之前的状态 // 回退:撤销选择,恢复到之前的状态
selected[i] = false; selected[i] = false;
@ -338,6 +344,7 @@ comments: true
duplicated.add(choice) # 记录选择过的元素值 duplicated.add(choice) # 记录选择过的元素值
selected[i] = True selected[i] = True
state.append(choice) state.append(choice)
# 进行下一轮选择
backtrack(state, choices, selected, res) backtrack(state, choices, selected, res)
# 回退:撤销选择,恢复到之前的状态 # 回退:撤销选择,恢复到之前的状态
selected[i] = False selected[i] = False
@ -402,6 +409,7 @@ comments: true
duplicated.Add(choice); // 记录选择过的元素值 duplicated.Add(choice); // 记录选择过的元素值
selected[i] = true; selected[i] = true;
state.Add(choice); state.Add(choice);
// 进行下一轮选择
backtrack(state, choices, selected, res); backtrack(state, choices, selected, res);
// 回退:撤销选择,恢复到之前的状态 // 回退:撤销选择,恢复到之前的状态
selected[i] = false; selected[i] = false;

View file

@ -202,6 +202,13 @@ comments: true
=== "C" === "C"
```c title="quick_sort.c" ```c title="quick_sort.c"
/* 元素交换 */
void swap(int nums[], int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* 快速排序类 */ /* 快速排序类 */
// 快速排序类-哨兵划分 // 快速排序类-哨兵划分
int partition(int nums[], int left, int right) { int partition(int nums[], int left, int right) {