mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-01 20:12:07 +08:00
Update the codes of backtracking.
This commit is contained in:
@ -25,6 +25,7 @@ def backtrack(
|
||||
# 尝试:做出选择,更新状态
|
||||
selected[i] = True
|
||||
state.append(choice)
|
||||
# 进行下一轮选择
|
||||
backtrack(state, choices, selected, res)
|
||||
# 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = False
|
||||
|
||||
@ -27,6 +27,7 @@ def backtrack(
|
||||
duplicated.add(choice) # 记录选择过的元素值
|
||||
selected[i] = True
|
||||
state.append(choice)
|
||||
# 进行下一轮选择
|
||||
backtrack(state, choices, selected, res)
|
||||
# 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = False
|
||||
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user