mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-03 10:53:35 +08:00
Update the codes of backtracking.
This commit is contained in:
@@ -21,6 +21,7 @@ void backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &sel
|
||||
// 尝试:做出选择,更新状态
|
||||
selected[i] = true;
|
||||
state.push_back(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
|
||||
@@ -23,6 +23,7 @@ void backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &sel
|
||||
duplicated.emplace(choice); // 记录选择过的元素值
|
||||
selected[i] = true;
|
||||
state.push_back(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
|
||||
@@ -45,6 +45,7 @@ void backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<ve
|
||||
if (isValid(state, choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state, choice);
|
||||
// 进行下一轮选择
|
||||
vector<TreeNode *> nextChoices{choice->left, choice->right};
|
||||
backtrack(state, nextChoices, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
|
||||
Reference in New Issue
Block a user