mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-05 04:04:22 +00:00
Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4 * Retranslate Japanese code with GPT-5.4
This commit is contained in:
@@ -7,31 +7,31 @@ Author: krahets (krahets@163.com)
|
||||
|
||||
def backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:
|
||||
"""バックトラッキング"""
|
||||
# n 段目に登ったとき、解の数に 1 を加える
|
||||
# 第 n 段に到達したら、方法数を 1 増やす
|
||||
if state == n:
|
||||
res[0] += 1
|
||||
# すべての選択肢を走査
|
||||
for choice in choices:
|
||||
# 枝刈り:n 段を超えて登ることを許可しない
|
||||
# 枝刈り: 第 n 段を超えないようにする
|
||||
if state + choice > n:
|
||||
continue
|
||||
# 試行:選択を行い、状態を更新
|
||||
# 試行: 選択を行い、状態を更新
|
||||
backtrack(choices, state + choice, n, res)
|
||||
# 撤回
|
||||
# バックトラック
|
||||
|
||||
|
||||
def climbing_stairs_backtrack(n: int) -> int:
|
||||
"""階段登り:バックトラッキング"""
|
||||
choices = [1, 2] # 1 段または 2 段登ることを選択可能
|
||||
state = 0 # 0 段目から登り始める
|
||||
res = [0] # res[0] を使用して解の数を記録
|
||||
choices = [1, 2] # 1 段または 2 段上ることを選べる
|
||||
state = 0 # 第 0 段から上り始める
|
||||
res = [0] # res[0] を使って方法数を記録する
|
||||
backtrack(choices, state, n, res)
|
||||
return res[0]
|
||||
|
||||
|
||||
"""ドライバーコード"""
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
n = 9
|
||||
|
||||
res = climbing_stairs_backtrack(n)
|
||||
print(f"{n} 段登り、合計 {res} 通りの解がある")
|
||||
print(f"{n} 段の階段を上る方法は全部で {res} 通りです")
|
||||
|
||||
Reference in New Issue
Block a user