mirror of
https://github.com/krahets/hello-algo.git
synced 2025-02-02 22:43:50 +08:00
Update the codes of backtracking.
This commit is contained in:
parent
9c070a028f
commit
bc77a81330
@ -21,6 +21,7 @@ void backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &sel
|
|||||||
// 尝试:做出选择,更新状态
|
// 尝试:做出选择,更新状态
|
||||||
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;
|
||||||
|
@ -23,6 +23,7 @@ void backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &sel
|
|||||||
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;
|
||||||
|
@ -45,6 +45,7 @@ void backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<ve
|
|||||||
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);
|
||||||
// 回退:撤销选择,恢复到之前的状态
|
// 回退:撤销选择,恢复到之前的状态
|
||||||
|
@ -25,6 +25,7 @@ public class permutations_i {
|
|||||||
// 尝试:做出选择,更新状态
|
// 尝试:做出选择,更新状态
|
||||||
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;
|
||||||
|
@ -27,6 +27,7 @@ public class permutations_ii {
|
|||||||
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;
|
||||||
|
@ -49,8 +49,8 @@ public class preorder_traversal_iii_template {
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ public class permutations_i {
|
|||||||
// 尝试:做出选择,更新状态
|
// 尝试:做出选择,更新状态
|
||||||
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;
|
||||||
|
@ -26,6 +26,7 @@ public class permutations_ii {
|
|||||||
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;
|
||||||
|
@ -49,6 +49,7 @@ public class preorder_traversal_iii_template {
|
|||||||
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);
|
||||||
|
@ -25,6 +25,7 @@ def backtrack(
|
|||||||
# 尝试:做出选择,更新状态
|
# 尝试:做出选择,更新状态
|
||||||
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
|
||||||
|
@ -27,6 +27,7 @@ def backtrack(
|
|||||||
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
|
||||||
|
@ -48,6 +48,7 @@ def backtrack(state: list[TreeNode], choices: list[TreeNode], res: list[list[Tre
|
|||||||
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user