docs: add Japanese translate documents (#1812)

* docs: add Japanese documents (`ja/docs`)

* docs: add Japanese documents (`ja/codes`)

* docs: add Japanese documents

* Remove pythontutor blocks in ja/

* Add an empty at the end of each markdown file.

* Add the missing figures (use the English version temporarily).

* Add index.md for Japanese version.

* Add index.html for Japanese version.

* Add missing index.assets

* Fix backtracking_algorithm.md for Japanese version.

* Add avatar_eltociear.jpg. Fix image links on the Japanese landing page.

* Add the Japanese banner.

---------

Co-authored-by: krahets <krahets@163.com>
This commit is contained in:
Ikko Eltociear Ashimine
2025-10-17 06:04:43 +09:00
committed by GitHub
parent 2487a27036
commit 954c45864b
886 changed files with 33569 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
/**
* File: n_queens.cpp
* Created Time: 2023-05-04
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* バックトラッキングアルゴリズムn クイーン */
void backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vector<string>>> &res, vector<bool> &cols,
vector<bool> &diags1, vector<bool> &diags2) {
// すべての行が配置されたら、解を記録
if (row == n) {
res.push_back(state);
return;
}
// すべての列を走査
for (int col = 0; col < n; col++) {
// セルに対応する主対角線と副対角線を計算
int diag1 = row - col + n - 1;
int diag2 = row + col;
// 剪定:セルの列、主対角線、副対角線にクイーンを配置することを許可しない
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 試行:セルにクイーンを配置
state[row][col] = "Q";
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 次の行を配置
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 回退:セルを空のスポットに復元
state[row][col] = "#";
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
/* n クイーンを解く */
vector<vector<vector<string>>> nQueens(int n) {
// n*n サイズのチェスボードを初期化、'Q' はクイーンを表し、'#' は空のスポットを表す
vector<vector<string>> state(n, vector<string>(n, "#"));
vector<bool> cols(n, false); // クイーンのある列を記録
vector<bool> diags1(2 * n - 1, false); // クイーンのある主対角線を記録
vector<bool> diags2(2 * n - 1, false); // クイーンのある副対角線を記録
vector<vector<vector<string>>> res;
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
/* ドライバーコード */
int main() {
int n = 4;
vector<vector<vector<string>>> res = nQueens(n);
cout << "チェスボードの次元を " << n << " として入力" << endl;
cout << "クイーン配置解の総数 = " << res.size() << endl;
for (const vector<vector<string>> &state : res) {
cout << "--------------------" << endl;
for (const vector<string> &row : state) {
printVector(row);
}
}
return 0;
}

View File

@@ -0,0 +1,54 @@
/**
* File: permutations_i.cpp
* Created Time: 2023-04-24
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* バックトラッキングアルゴリズム:順列 I */
void backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {
// 状態の長さが要素数と等しくなったら、解を記録
if (state.size() == choices.size()) {
res.push_back(state);
return;
}
// すべての選択肢を走査
for (int i = 0; i < choices.size(); i++) {
int choice = choices[i];
// 剪定:要素の重複選択を許可しない
if (!selected[i]) {
// 試行:選択を行い、状態を更新
selected[i] = true;
state.push_back(choice);
// 次のラウンドの選択に進む
backtrack(state, choices, selected, res);
// 回退:選択を取り消し、前の状態に復元
selected[i] = false;
state.pop_back();
}
}
}
/* 順列 I */
vector<vector<int>> permutationsI(vector<int> nums) {
vector<int> state;
vector<bool> selected(nums.size(), false);
vector<vector<int>> res;
backtrack(state, nums, selected, res);
return res;
}
/* ドライバーコード */
int main() {
vector<int> nums = {1, 2, 3};
vector<vector<int>> res = permutationsI(nums);
cout << "入力配列 nums = ";
printVector(nums);
cout << "すべての順列 res = ";
printVectorMatrix(res);
return 0;
}

View File

@@ -0,0 +1,56 @@
/**
* File: permutations_ii.cpp
* Created Time: 2023-04-24
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* バックトラッキングアルゴリズム:順列 II */
void backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {
// 状態の長さが要素数と等しくなったら、解を記録
if (state.size() == choices.size()) {
res.push_back(state);
return;
}
// すべての選択肢を走査
unordered_set<int> duplicated;
for (int i = 0; i < choices.size(); i++) {
int choice = choices[i];
// 剪定:要素の重複選択を許可せず、等しい要素の重複選択も許可しない
if (!selected[i] && duplicated.find(choice) == duplicated.end()) {
// 試行:選択を行い、状態を更新
duplicated.emplace(choice); // 選択された要素値を記録
selected[i] = true;
state.push_back(choice);
// 次のラウンドの選択に進む
backtrack(state, choices, selected, res);
// 回退:選択を取り消し、前の状態に復元
selected[i] = false;
state.pop_back();
}
}
}
/* 順列 II */
vector<vector<int>> permutationsII(vector<int> nums) {
vector<int> state;
vector<bool> selected(nums.size(), false);
vector<vector<int>> res;
backtrack(state, nums, selected, res);
return res;
}
/* ドライバーコード */
int main() {
vector<int> nums = {1, 1, 2};
vector<vector<int>> res = permutationsII(nums);
cout << "入力配列 nums = ";
printVector(nums);
cout << "すべての順列 res = ";
printVectorMatrix(res);
return 0;
}

View File

@@ -0,0 +1,43 @@
/**
* File: preorder_traversal_i_compact.cpp
* Created Time: 2023-04-16
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
vector<TreeNode *> res;
/* 前順走査:例1 */
void preOrder(TreeNode *root) {
if (root == nullptr) {
return;
}
if (root->val == 7) {
// 解を記録
res.push_back(root);
}
preOrder(root->left);
preOrder(root->right);
}
/* ドライバーコード */
int main() {
vector<int> arr = {1, 7, 3, 4, 5, 6, 7};
TreeNode *root = vecToTree(arr);
cout << "\n二分木を初期化" << endl;
printTree(root);
// 前順走査
res.clear();
preOrder(root);
cout << "\n値7のードをすべて出力" << endl;
vector<int> vals;
for (TreeNode *node : res) {
vals.push_back(node->val);
}
printVector(vals);
return 0;
}

View File

@@ -0,0 +1,51 @@
/**
* File: preorder_traversal_ii_compact.cpp
* Created Time: 2023-04-16
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
vector<TreeNode *> path;
vector<vector<TreeNode *>> res;
/* 前順走査:例2 */
void preOrder(TreeNode *root) {
if (root == nullptr) {
return;
}
// 試行
path.push_back(root);
if (root->val == 7) {
// 解を記録
res.push_back(path);
}
preOrder(root->left);
preOrder(root->right);
// 回退
path.pop_back();
}
/* ドライバーコード */
int main() {
vector<int> arr = {1, 7, 3, 4, 5, 6, 7};
TreeNode *root = vecToTree(arr);
cout << "\n二分木を初期化" << endl;
printTree(root);
// 前順走査
path.clear();
res.clear();
preOrder(root);
cout << "\nルートからード7までのすべてのパスを出力" << endl;
for (vector<TreeNode *> &path : res) {
vector<int> vals;
for (TreeNode *node : path) {
vals.push_back(node->val);
}
printVector(vals);
}
return 0;
}

View File

@@ -0,0 +1,52 @@
/**
* File: preorder_traversal_iii_compact.cpp
* Created Time: 2023-04-16
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
vector<TreeNode *> path;
vector<vector<TreeNode *>> res;
/* 前順走査:例3 */
void preOrder(TreeNode *root) {
// 剪定
if (root == nullptr || root->val == 3) {
return;
}
// 試行
path.push_back(root);
if (root->val == 7) {
// 解を記録
res.push_back(path);
}
preOrder(root->left);
preOrder(root->right);
// 回退
path.pop_back();
}
/* ドライバーコード */
int main() {
vector<int> arr = {1, 7, 3, 4, 5, 6, 7};
TreeNode *root = vecToTree(arr);
cout << "\n二分木を初期化" << endl;
printTree(root);
// 前順走査
path.clear();
res.clear();
preOrder(root);
cout << "\nルートからード7までのすべてのパスを出力、値3のードは含まない" << endl;
for (vector<TreeNode *> &path : res) {
vector<int> vals;
for (TreeNode *node : path) {
vals.push_back(node->val);
}
printVector(vals);
}
return 0;
}

View File

@@ -0,0 +1,79 @@
/**
* File: preorder_traversal_iii_template.cpp
* Created Time: 2023-04-16
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* 現在の状態が解かどうかを判定 */
bool isSolution(vector<TreeNode *> &state) {
return !state.empty() && state.back()->val == 7;
}
/* 解を記録 */
void recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {
res.push_back(state);
}
/* 現在の状態下で選択が合法かどうかを判定 */
bool isValid(vector<TreeNode *> &state, TreeNode *choice) {
return choice != nullptr && choice->val != 3;
}
/* 状態を更新 */
void makeChoice(vector<TreeNode *> &state, TreeNode *choice) {
state.push_back(choice);
}
/* 状態を復元 */
void undoChoice(vector<TreeNode *> &state, TreeNode *choice) {
state.pop_back();
}
/* バックトラッキングアルゴリズム:例3 */
void backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) {
// 解かどうかをチェック
if (isSolution(state)) {
// 解を記録
recordSolution(state, res);
}
// すべての選択肢を走査
for (TreeNode *choice : choices) {
// 剪定:選択が合法かどうかをチェック
if (isValid(state, choice)) {
// 試行:選択を行い、状態を更新
makeChoice(state, choice);
// 次のラウンドの選択に進む
vector<TreeNode *> nextChoices{choice->left, choice->right};
backtrack(state, nextChoices, res);
// 回退:選択を取り消し、前の状態に復元
undoChoice(state, choice);
}
}
}
/* ドライバーコード */
int main() {
vector<int> arr = {1, 7, 3, 4, 5, 6, 7};
TreeNode *root = vecToTree(arr);
cout << "\n二分木を初期化" << endl;
printTree(root);
// バックトラッキングアルゴリズム
vector<TreeNode *> state;
vector<TreeNode *> choices = {root};
vector<vector<TreeNode *>> res;
backtrack(state, choices, res);
cout << "\nルートからード7までのすべてのパスを出力、パスには値3のードを含まないことが要求される" << endl;
for (vector<TreeNode *> &path : res) {
vector<int> vals;
for (TreeNode *node : path) {
vals.push_back(node->val);
}
printVector(vals);
}
return 0;
}

View File

@@ -0,0 +1,57 @@
/**
* File: subset_sum_i.cpp
* Created Time: 2023-06-21
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* バックトラッキングアルゴリズム:部分集合和 I */
void backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {
// 部分集合の和がtargetと等しいとき、解を記録
if (target == 0) {
res.push_back(state);
return;
}
// すべての選択肢を走査
// 剪定二startから走査を開始し、重複する部分集合の生成を回避
for (int i = start; i < choices.size(); i++) {
// 剪定一部分集合の和がtargetを超えた場合、即座にループを終了
// 配列がソートされているため、後の要素はさらに大きく、部分集合の和は必ずtargetを超える
if (target - choices[i] < 0) {
break;
}
// 試行選択を行い、target、startを更新
state.push_back(choices[i]);
// 次のラウンドの選択に進む
backtrack(state, target - choices[i], choices, i, res);
// 回退:選択を取り消し、前の状態に復元
state.pop_back();
}
}
/* 部分集合和 I を解く */
vector<vector<int>> subsetSumI(vector<int> nums, int target) {
vector<int> state; // 状態(部分集合)
sort(nums.begin(), nums.end()); // nums をソート
int start = 0; // 走査の開始点
vector<vector<int>> res; // 結果リスト(部分集合リスト)
backtrack(state, target, nums, start, res);
return res;
}
/* ドライバーコード */
int main() {
vector<int> nums = {3, 4, 5};
int target = 9;
vector<vector<int>> res = subsetSumI(nums, target);
cout << "入力配列 nums = ";
printVector(nums);
cout << "target = " << target << endl;
cout << "和が " << target << " のすべての部分集合 res = " << endl;
printVectorMatrix(res);
return 0;
}

View File

@@ -0,0 +1,55 @@
/**
* File: subset_sum_i_naive.cpp
* Created Time: 2023-06-21
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* バックトラッキングアルゴリズム:部分集合和 I */
void backtrack(vector<int> &state, int target, int total, vector<int> &choices, vector<vector<int>> &res) {
// 部分集合の和がtargetと等しいとき、解を記録
if (total == target) {
res.push_back(state);
return;
}
// すべての選択肢を走査
for (int i = 0; i < choices.size(); i++) {
// 剪定部分集合の和がtargetを超えた場合、その選択をスキップ
if (total + choices[i] > target) {
continue;
}
// 試行選択を行い、要素とtotalを更新
state.push_back(choices[i]);
// 次のラウンドの選択に進む
backtrack(state, target, total + choices[i], choices, res);
// 回退:選択を取り消し、前の状態に復元
state.pop_back();
}
}
/* 部分集合和 I を解く(重複する部分集合を含む) */
vector<vector<int>> subsetSumINaive(vector<int> nums, int target) {
vector<int> state; // 状態(部分集合)
int total = 0; // 部分集合の和
vector<vector<int>> res; // 結果リスト(部分集合リスト)
backtrack(state, target, total, nums, res);
return res;
}
/* ドライバーコード */
int main() {
vector<int> nums = {3, 4, 5};
int target = 9;
vector<vector<int>> res = subsetSumINaive(nums, target);
cout << "入力配列 nums = ";
printVector(nums);
cout << "target = " << target << endl;
cout << "和が " << target << " のすべての部分集合 res = " << endl;
printVectorMatrix(res);
cout << "この方法の結果には重複する集合が含まれています" << endl;
return 0;
}

View File

@@ -0,0 +1,62 @@
/**
* File: subset_sum_ii.cpp
* Created Time: 2023-06-21
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* バックトラッキングアルゴリズム:部分集合和 II */
void backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {
// 部分集合の和がtargetと等しいとき、解を記録
if (target == 0) {
res.push_back(state);
return;
}
// すべての選択肢を走査
// 剪定二startから走査を開始し、重複する部分集合の生成を回避
// 剪定三startから走査を開始し、同じ要素の繰り返し選択を回避
for (int i = start; i < choices.size(); i++) {
// 剪定一部分集合の和がtargetを超えた場合、即座にループを終了
// 配列がソートされているため、後の要素はさらに大きく、部分集合の和は必ずtargetを超える
if (target - choices[i] < 0) {
break;
}
// 剪定四:要素が左の要素と等しい場合、検索ブランチの重複を示すのでスキップ
if (i > start && choices[i] == choices[i - 1]) {
continue;
}
// 試行選択を行い、target、startを更新
state.push_back(choices[i]);
// 次のラウンドの選択に進む
backtrack(state, target - choices[i], choices, i + 1, res);
// 回退:選択を取り消し、前の状態に復元
state.pop_back();
}
}
/* 部分集合和 II を解く */
vector<vector<int>> subsetSumII(vector<int> nums, int target) {
vector<int> state; // 状態(部分集合)
sort(nums.begin(), nums.end()); // nums をソート
int start = 0; // 走査の開始点
vector<vector<int>> res; // 結果リスト(部分集合リスト)
backtrack(state, target, nums, start, res);
return res;
}
/* ドライバーコード */
int main() {
vector<int> nums = {4, 4, 5};
int target = 9;
vector<vector<int>> res = subsetSumII(nums, target);
cout << "入力配列 nums = ";
printVector(nums);
cout << "target = " << target << endl;
cout << "和が " << target << " のすべての部分集合 res = " << endl;
printVectorMatrix(res);
return 0;
}