From bc77a813304d7a2bd133f983ae7d52d2a90ebf3d Mon Sep 17 00:00:00 2001 From: krahets Date: Thu, 27 Apr 2023 02:17:04 +0800 Subject: [PATCH] Update the codes of backtracking. --- codes/cpp/chapter_backtracking/permutations_i.cpp | 1 + codes/cpp/chapter_backtracking/permutations_ii.cpp | 1 + .../chapter_backtracking/preorder_traversal_iii_template.cpp | 1 + codes/csharp/chapter_backtracking/permutations_i.cs | 1 + codes/csharp/chapter_backtracking/permutations_ii.cs | 1 + .../chapter_backtracking/preorder_traversal_iii_template.cs | 4 ++-- codes/java/chapter_backtracking/permutations_i.java | 1 + codes/java/chapter_backtracking/permutations_ii.java | 1 + .../chapter_backtracking/preorder_traversal_iii_template.java | 1 + codes/python/chapter_backtracking/permutations_i.py | 1 + codes/python/chapter_backtracking/permutations_ii.py | 1 + .../chapter_backtracking/preorder_traversal_iii_template.py | 1 + 12 files changed, 13 insertions(+), 2 deletions(-) diff --git a/codes/cpp/chapter_backtracking/permutations_i.cpp b/codes/cpp/chapter_backtracking/permutations_i.cpp index f20505caf..c49867e20 100644 --- a/codes/cpp/chapter_backtracking/permutations_i.cpp +++ b/codes/cpp/chapter_backtracking/permutations_i.cpp @@ -21,6 +21,7 @@ void backtrack(vector &state, const vector &choices, vector &sel // 尝试:做出选择,更新状态 selected[i] = true; state.push_back(choice); + // 进行下一轮选择 backtrack(state, choices, selected, res); // 回退:撤销选择,恢复到之前的状态 selected[i] = false; diff --git a/codes/cpp/chapter_backtracking/permutations_ii.cpp b/codes/cpp/chapter_backtracking/permutations_ii.cpp index 21144d1bf..6a20cca1f 100644 --- a/codes/cpp/chapter_backtracking/permutations_ii.cpp +++ b/codes/cpp/chapter_backtracking/permutations_ii.cpp @@ -23,6 +23,7 @@ void backtrack(vector &state, const vector &choices, vector &sel duplicated.emplace(choice); // 记录选择过的元素值 selected[i] = true; state.push_back(choice); + // 进行下一轮选择 backtrack(state, choices, selected, res); // 回退:撤销选择,恢复到之前的状态 selected[i] = false; diff --git a/codes/cpp/chapter_backtracking/preorder_traversal_iii_template.cpp b/codes/cpp/chapter_backtracking/preorder_traversal_iii_template.cpp index 9c86422b2..1d043b583 100644 --- a/codes/cpp/chapter_backtracking/preorder_traversal_iii_template.cpp +++ b/codes/cpp/chapter_backtracking/preorder_traversal_iii_template.cpp @@ -45,6 +45,7 @@ void backtrack(vector &state, vector &choices, vector nextChoices{choice->left, choice->right}; backtrack(state, nextChoices, res); // 回退:撤销选择,恢复到之前的状态 diff --git a/codes/csharp/chapter_backtracking/permutations_i.cs b/codes/csharp/chapter_backtracking/permutations_i.cs index 3b41b5277..3553151b9 100644 --- a/codes/csharp/chapter_backtracking/permutations_i.cs +++ b/codes/csharp/chapter_backtracking/permutations_i.cs @@ -25,6 +25,7 @@ public class permutations_i { // 尝试:做出选择,更新状态 selected[i] = true; state.Add(choice); + // 进行下一轮选择 backtrack(state, choices, selected, res); // 回退:撤销选择,恢复到之前的状态 selected[i] = false; diff --git a/codes/csharp/chapter_backtracking/permutations_ii.cs b/codes/csharp/chapter_backtracking/permutations_ii.cs index 5c4cf0cf0..e0ffeacbe 100644 --- a/codes/csharp/chapter_backtracking/permutations_ii.cs +++ b/codes/csharp/chapter_backtracking/permutations_ii.cs @@ -27,6 +27,7 @@ public class permutations_ii { duplicated.Add(choice); // 记录选择过的元素值 selected[i] = true; state.Add(choice); + // 进行下一轮选择 backtrack(state, choices, selected, res); // 回退:撤销选择,恢复到之前的状态 selected[i] = false; diff --git a/codes/csharp/chapter_backtracking/preorder_traversal_iii_template.cs b/codes/csharp/chapter_backtracking/preorder_traversal_iii_template.cs index 92a44c80e..32824d697 100644 --- a/codes/csharp/chapter_backtracking/preorder_traversal_iii_template.cs +++ b/codes/csharp/chapter_backtracking/preorder_traversal_iii_template.cs @@ -49,8 +49,8 @@ public class preorder_traversal_iii_template { if (isValid(state, choice)) { // 尝试:做出选择,更新状态 makeChoice(state, choice); - List nextChoices = new List() { choice.left, choice.right }; - backtrack(state, nextChoices, res); + // 进行下一轮选择 + backtrack(state, new List { choice.left, choice.right }, res); // 回退:撤销选择,恢复到之前的状态 undoChoice(state, choice); } diff --git a/codes/java/chapter_backtracking/permutations_i.java b/codes/java/chapter_backtracking/permutations_i.java index 19b979b33..b4d104e06 100644 --- a/codes/java/chapter_backtracking/permutations_i.java +++ b/codes/java/chapter_backtracking/permutations_i.java @@ -24,6 +24,7 @@ public class permutations_i { // 尝试:做出选择,更新状态 selected[i] = true; state.add(choice); + // 进行下一轮选择 backtrack(state, choices, selected, res); // 回退:撤销选择,恢复到之前的状态 selected[i] = false; diff --git a/codes/java/chapter_backtracking/permutations_ii.java b/codes/java/chapter_backtracking/permutations_ii.java index 300bcc797..77353d5e3 100644 --- a/codes/java/chapter_backtracking/permutations_ii.java +++ b/codes/java/chapter_backtracking/permutations_ii.java @@ -26,6 +26,7 @@ public class permutations_ii { duplicated.add(choice); // 记录选择过的元素值 selected[i] = true; state.add(choice); + // 进行下一轮选择 backtrack(state, choices, selected, res); // 回退:撤销选择,恢复到之前的状态 selected[i] = false; diff --git a/codes/java/chapter_backtracking/preorder_traversal_iii_template.java b/codes/java/chapter_backtracking/preorder_traversal_iii_template.java index 27685a1ec..61779165b 100644 --- a/codes/java/chapter_backtracking/preorder_traversal_iii_template.java +++ b/codes/java/chapter_backtracking/preorder_traversal_iii_template.java @@ -49,6 +49,7 @@ public class preorder_traversal_iii_template { if (isValid(state, choice)) { // 尝试:做出选择,更新状态 makeChoice(state, choice); + // 进行下一轮选择 backtrack(state, Arrays.asList(choice.left, choice.right), res); // 回退:撤销选择,恢复到之前的状态 undoChoice(state, choice); diff --git a/codes/python/chapter_backtracking/permutations_i.py b/codes/python/chapter_backtracking/permutations_i.py index 3b36445b2..d5c29d6c2 100644 --- a/codes/python/chapter_backtracking/permutations_i.py +++ b/codes/python/chapter_backtracking/permutations_i.py @@ -25,6 +25,7 @@ def backtrack( # 尝试:做出选择,更新状态 selected[i] = True state.append(choice) + # 进行下一轮选择 backtrack(state, choices, selected, res) # 回退:撤销选择,恢复到之前的状态 selected[i] = False diff --git a/codes/python/chapter_backtracking/permutations_ii.py b/codes/python/chapter_backtracking/permutations_ii.py index 552a16b0c..fac0b37e1 100644 --- a/codes/python/chapter_backtracking/permutations_ii.py +++ b/codes/python/chapter_backtracking/permutations_ii.py @@ -27,6 +27,7 @@ def backtrack( duplicated.add(choice) # 记录选择过的元素值 selected[i] = True state.append(choice) + # 进行下一轮选择 backtrack(state, choices, selected, res) # 回退:撤销选择,恢复到之前的状态 selected[i] = False diff --git a/codes/python/chapter_backtracking/preorder_traversal_iii_template.py b/codes/python/chapter_backtracking/preorder_traversal_iii_template.py index b6b9e9afc..a4c88a959 100644 --- a/codes/python/chapter_backtracking/preorder_traversal_iii_template.py +++ b/codes/python/chapter_backtracking/preorder_traversal_iii_template.py @@ -48,6 +48,7 @@ def backtrack(state: list[TreeNode], choices: list[TreeNode], res: list[list[Tre if is_valid(state, choice): # 尝试:做出选择,更新状态 make_choice(state, choice) + # 进行下一轮选择 backtrack(state, [choice.left, choice.right], res) # 回退:撤销选择,恢复到之前的状态 undo_choice(state, choice)