mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-01 10:04:23 +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:
@@ -1,3 +1,4 @@
|
||||
|
||||
/**
|
||||
* File: climbing_stairs_backtrack.cpp
|
||||
* Created Time: 2023-06-30
|
||||
@@ -8,35 +9,35 @@
|
||||
|
||||
/* バックトラッキング */
|
||||
void backtrack(vector<int> &choices, int state, int n, vector<int> &res) {
|
||||
// n段目に到達したとき、解の数に1を加える
|
||||
// 第 n 段に到達したら、方法数を 1 増やす
|
||||
if (state == n)
|
||||
res[0]++;
|
||||
// すべての選択肢を走査
|
||||
for (auto &choice : choices) {
|
||||
// 剪定:n段を超えて登ることを許可しない
|
||||
// 枝刈り: 第 n 段を超えないようにする
|
||||
if (state + choice > n)
|
||||
continue;
|
||||
// 試行:選択を行い、状態を更新
|
||||
// 試行: 選択を行い、状態を更新
|
||||
backtrack(choices, state + choice, n, res);
|
||||
// 撤回
|
||||
// バックトラック
|
||||
}
|
||||
}
|
||||
|
||||
/* 階段登り:バックトラッキング */
|
||||
int climbingStairsBacktrack(int n) {
|
||||
vector<int> choices = {1, 2}; // 1段または2段登ることを選択可能
|
||||
int state = 0; // 0段目から登り始める
|
||||
vector<int> res = {0}; // res[0] を使用して解の数を記録
|
||||
vector<int> choices = {1, 2}; // 1 段または 2 段上ることを選べる
|
||||
int state = 0; // 第 0 段から上り始める
|
||||
vector<int> res = {0}; // res[0] を使って方法数を記録する
|
||||
backtrack(choices, state, n, res);
|
||||
return res[0];
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int n = 9;
|
||||
|
||||
int res = climbingStairsBacktrack(n);
|
||||
cout << n << "段の階段を登る解は" << res << "通りです" << endl;
|
||||
cout << "階段を " << n << " 段上る方法は全部で " << res << " 通り" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user