mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Bug fixes and improvements (#1380)
* preorder, inorder, postorder -> pre-order, in-order, post-order * Bug fixes * Bug fixes * Update what_is_dsa.md * Sync zh and zh-hant versions * Sync zh and zh-hant versions. * Update performance_evaluation.md and time_complexity.md * Add @khoaxuantu to the landing page. * Sync zh and zh-hant versions * Add @ khoaxuantu to the landing page of zh-hant and en versions.
This commit is contained in:
@ -406,7 +406,27 @@
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title=""
|
||||
### 回溯演算法框架 ###
|
||||
def backtrack(state, choices, res)
|
||||
# 判斷是否為解
|
||||
if is_solution?(state)
|
||||
# 記錄解
|
||||
record_solution(state, res)
|
||||
return
|
||||
end
|
||||
|
||||
# 走訪所有選擇
|
||||
for choice in choices
|
||||
# 剪枝:判斷選擇是否合法
|
||||
if is_valid?(state, choice)
|
||||
# 嘗試:做出選擇,更新狀態
|
||||
make_choice(state, choice)
|
||||
backtrack(state, choices, res)
|
||||
# 回退:撤銷選擇,恢復到之前的狀態
|
||||
undo_choice(state, choice)
|
||||
end
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
Reference in New Issue
Block a user