mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-09 14:06:06 +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:
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* File: n_queens.cs
|
||||
* Created Time: 2023-05-04
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class n_queens {
|
||||
/* バックトラッキング:N クイーン */
|
||||
void Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,
|
||||
bool[] cols, bool[] diags1, bool[] diags2) {
|
||||
// すべての行への配置が完了したら、解を記録する
|
||||
if (row == n) {
|
||||
List<List<string>> copyState = [];
|
||||
foreach (List<string> sRow in state) {
|
||||
copyState.Add(new List<string>(sRow));
|
||||
}
|
||||
res.Add(copyState);
|
||||
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 クイーンを解く */
|
||||
List<List<List<string>>> NQueens(int n) {
|
||||
// n*n の盤面を初期化する。'Q' はクイーン、'#' は空きマスを表す
|
||||
List<List<string>> state = [];
|
||||
for (int i = 0; i < n; i++) {
|
||||
List<string> row = [];
|
||||
for (int j = 0; j < n; j++) {
|
||||
row.Add("#");
|
||||
}
|
||||
state.Add(row);
|
||||
}
|
||||
bool[] cols = new bool[n]; // 列にクイーンがあるか記録
|
||||
bool[] diags1 = new bool[2 * n - 1]; // 主対角線にクイーンがあるかを記録
|
||||
bool[] diags2 = new bool[2 * n - 1]; // 副対角線にクイーンがあるかを記録
|
||||
List<List<List<string>>> res = [];
|
||||
|
||||
Backtrack(0, n, state, res, cols, diags1, diags2);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 4;
|
||||
List<List<List<string>>> res = NQueens(n);
|
||||
|
||||
Console.WriteLine("盤面の縦横サイズの入力値は " + n);
|
||||
Console.WriteLine("クイーンの配置パターンは全部で " + res.Count + " 通り");
|
||||
foreach (List<List<string>> state in res) {
|
||||
Console.WriteLine("--------------------");
|
||||
foreach (List<string> row in state) {
|
||||
PrintUtil.PrintList(row);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* File: permutations_i.cs
|
||||
* Created Time: 2023-04-24
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class permutations_i {
|
||||
/* バックトラッキング:順列 I */
|
||||
void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
|
||||
// 状態の長さが要素数に等しければ、解を記録
|
||||
if (state.Count == choices.Length) {
|
||||
res.Add(new List<int>(state));
|
||||
return;
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
for (int i = 0; i < choices.Length; i++) {
|
||||
int choice = choices[i];
|
||||
// 枝刈り:要素の重複選択を許可しない
|
||||
if (!selected[i]) {
|
||||
// 試行: 選択を行い、状態を更新
|
||||
selected[i] = true;
|
||||
state.Add(choice);
|
||||
// 次の選択へ進む
|
||||
Backtrack(state, choices, selected, res);
|
||||
// バックトラック:選択を取り消し、前の状態に戻す
|
||||
selected[i] = false;
|
||||
state.RemoveAt(state.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 全順列 I */
|
||||
List<List<int>> PermutationsI(int[] nums) {
|
||||
List<List<int>> res = [];
|
||||
Backtrack([], nums, new bool[nums.Length], res);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [1, 2, 3];
|
||||
|
||||
List<List<int>> res = PermutationsI(nums);
|
||||
|
||||
Console.WriteLine("入力配列 nums = " + string.Join(", ", nums));
|
||||
Console.WriteLine("すべての順列 res = ");
|
||||
foreach (List<int> permutation in res) {
|
||||
PrintUtil.PrintList(permutation);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* File: permutations_ii.cs
|
||||
* Created Time: 2023-04-24
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class permutations_ii {
|
||||
/* バックトラッキング:順列 II */
|
||||
void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
|
||||
// 状態の長さが要素数に等しければ、解を記録
|
||||
if (state.Count == choices.Length) {
|
||||
res.Add(new List<int>(state));
|
||||
return;
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
HashSet<int> duplicated = [];
|
||||
for (int i = 0; i < choices.Length; i++) {
|
||||
int choice = choices[i];
|
||||
// 枝刈り:要素の重複選択を許可せず、同値要素の重複選択も許可しない
|
||||
if (!selected[i] && !duplicated.Contains(choice)) {
|
||||
// 試行: 選択を行い、状態を更新
|
||||
duplicated.Add(choice); // 選択済みの要素値を記録
|
||||
selected[i] = true;
|
||||
state.Add(choice);
|
||||
// 次の選択へ進む
|
||||
Backtrack(state, choices, selected, res);
|
||||
// バックトラック:選択を取り消し、前の状態に戻す
|
||||
selected[i] = false;
|
||||
state.RemoveAt(state.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 全順列 II */
|
||||
List<List<int>> PermutationsII(int[] nums) {
|
||||
List<List<int>> res = [];
|
||||
Backtrack([], nums, new bool[nums.Length], res);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [1, 2, 2];
|
||||
|
||||
List<List<int>> res = PermutationsII(nums);
|
||||
|
||||
Console.WriteLine("入力配列 nums = " + string.Join(", ", nums));
|
||||
Console.WriteLine("すべての順列 res = ");
|
||||
foreach (List<int> permutation in res) {
|
||||
PrintUtil.PrintList(permutation);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* File: preorder_traversal_i_compact.cs
|
||||
* Created Time: 2023-04-17
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class preorder_traversal_i_compact {
|
||||
List<TreeNode> res = [];
|
||||
|
||||
/* 前順走査:例題 1 */
|
||||
void PreOrder(TreeNode? root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
if (root.val == 7) {
|
||||
// 解を記録
|
||||
res.Add(root);
|
||||
}
|
||||
PreOrder(root.left);
|
||||
PreOrder(root.right);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\n二分木を初期化");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// 先行順走査
|
||||
PreOrder(root);
|
||||
|
||||
Console.WriteLine("\n値が 7 のノードをすべて出力");
|
||||
PrintUtil.PrintList(res.Select(p => p.val).ToList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: preorder_traversal_ii_compact.cs
|
||||
* Created Time: 2023-04-17
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class preorder_traversal_ii_compact {
|
||||
List<TreeNode> path = [];
|
||||
List<List<TreeNode>> res = [];
|
||||
|
||||
/* 前順走査:例題 2 */
|
||||
void PreOrder(TreeNode? root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
// 試す
|
||||
path.Add(root);
|
||||
if (root.val == 7) {
|
||||
// 解を記録
|
||||
res.Add(new List<TreeNode>(path));
|
||||
}
|
||||
PreOrder(root.left);
|
||||
PreOrder(root.right);
|
||||
// バックトラック
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\n二分木を初期化");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// 先行順走査
|
||||
PreOrder(root);
|
||||
|
||||
Console.WriteLine("\nルートノードからノード 7 までのすべての経路を出力");
|
||||
foreach (List<TreeNode> path in res) {
|
||||
PrintUtil.PrintList(path.Select(p => p.val).ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* File: preorder_traversal_iii_compact.cs
|
||||
* Created Time: 2023-04-17
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class preorder_traversal_iii_compact {
|
||||
List<TreeNode> path = [];
|
||||
List<List<TreeNode>> res = [];
|
||||
|
||||
/* 前順走査:例題 3 */
|
||||
void PreOrder(TreeNode? root) {
|
||||
// 枝刈り
|
||||
if (root == null || root.val == 3) {
|
||||
return;
|
||||
}
|
||||
// 試す
|
||||
path.Add(root);
|
||||
if (root.val == 7) {
|
||||
// 解を記録
|
||||
res.Add(new List<TreeNode>(path));
|
||||
}
|
||||
PreOrder(root.left);
|
||||
PreOrder(root.right);
|
||||
// バックトラック
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\n二分木を初期化");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// 先行順走査
|
||||
PreOrder(root);
|
||||
|
||||
Console.WriteLine("\nルートノードからノード 7 までのすべての経路を出力し、経路には値が 3 のノードを含めない");
|
||||
foreach (List<TreeNode> path in res) {
|
||||
PrintUtil.PrintList(path.Select(p => p.val).ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* File: preorder_traversal_iii_template.cs
|
||||
* Created Time: 2023-04-17
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class preorder_traversal_iii_template {
|
||||
/* 現在の状態が解かどうかを判定 */
|
||||
bool IsSolution(List<TreeNode> state) {
|
||||
return state.Count != 0 && state[^1].val == 7;
|
||||
}
|
||||
|
||||
/* 解を記録 */
|
||||
void RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
|
||||
res.Add(new List<TreeNode>(state));
|
||||
}
|
||||
|
||||
/* 現在の状態で、この選択が有効かどうかを判定 */
|
||||
bool IsValid(List<TreeNode> state, TreeNode choice) {
|
||||
return choice != null && choice.val != 3;
|
||||
}
|
||||
|
||||
/* 状態を更新 */
|
||||
void MakeChoice(List<TreeNode> state, TreeNode choice) {
|
||||
state.Add(choice);
|
||||
}
|
||||
|
||||
/* 状態を元に戻す */
|
||||
void UndoChoice(List<TreeNode> state, TreeNode choice) {
|
||||
state.RemoveAt(state.Count - 1);
|
||||
}
|
||||
|
||||
/* バックトラッキング:例題 3 */
|
||||
void Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
|
||||
// 解かどうかを確認
|
||||
if (IsSolution(state)) {
|
||||
// 解を記録
|
||||
RecordSolution(state, res);
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
foreach (TreeNode choice in choices) {
|
||||
// 枝刈り:選択が妥当かを確認する
|
||||
if (IsValid(state, choice)) {
|
||||
// 試行: 選択を行い、状態を更新
|
||||
MakeChoice(state, choice);
|
||||
// 次の選択へ進む
|
||||
Backtrack(state, [choice.left!, choice.right!], res);
|
||||
// バックトラック:選択を取り消し、前の状態に戻す
|
||||
UndoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\n二分木を初期化");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// バックトラッキング法
|
||||
List<List<TreeNode>> res = [];
|
||||
List<TreeNode> choices = [root!];
|
||||
Backtrack([], choices, res);
|
||||
|
||||
Console.WriteLine("\nルートノードからノード 7 までのすべての経路を出力し、経路に値が 3 のノードを含まないことを条件とする");
|
||||
foreach (List<TreeNode> path in res) {
|
||||
PrintUtil.PrintList(path.Select(p => p.val).ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* File: subset_sum_i.cs
|
||||
* Created Time: 2023-06-25
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class subset_sum_i {
|
||||
/* バックトラッキング:部分和 I */
|
||||
void Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
|
||||
// 部分集合の和が target に等しければ、解を記録
|
||||
if (target == 0) {
|
||||
res.Add(new List<int>(state));
|
||||
return;
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
// 枝刈り 2: start から走査し、重複する部分集合の生成を避ける
|
||||
for (int i = start; i < choices.Length; i++) {
|
||||
// 枝刈り1:部分集合の和が target を超えたら、直ちにループを終了する
|
||||
// 配列はソート済みで後続要素のほうが大きく、部分集合の和は必ず target を超えるため
|
||||
if (target - choices[i] < 0) {
|
||||
break;
|
||||
}
|
||||
// 試す:選択を行い、target と start を更新
|
||||
state.Add(choices[i]);
|
||||
// 次の選択へ進む
|
||||
Backtrack(state, target - choices[i], choices, i, res);
|
||||
// バックトラック:選択を取り消し、前の状態に戻す
|
||||
state.RemoveAt(state.Count - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* 部分和 I を解く */
|
||||
List<List<int>> SubsetSumI(int[] nums, int target) {
|
||||
List<int> state = []; // 状態(部分集合)
|
||||
Array.Sort(nums); // nums をソート
|
||||
int start = 0; // 開始点を走査
|
||||
List<List<int>> res = []; // 結果リスト(部分集合のリスト)
|
||||
Backtrack(state, target, nums, start, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [3, 4, 5];
|
||||
int target = 9;
|
||||
List<List<int>> res = SubsetSumI(nums, target);
|
||||
Console.WriteLine("入力配列 nums = " + string.Join(", ", nums) + ", target = " + target);
|
||||
Console.WriteLine("和が " + target + " に等しいすべての部分集合 res = ");
|
||||
foreach (var subset in res) {
|
||||
PrintUtil.PrintList(subset);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* File: subset_sum_i_naive.cs
|
||||
* Created Time: 2023-06-25
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class subset_sum_i_naive {
|
||||
/* バックトラッキング:部分和 I */
|
||||
void Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {
|
||||
// 部分集合の和が target に等しければ、解を記録
|
||||
if (total == target) {
|
||||
res.Add(new List<int>(state));
|
||||
return;
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
for (int i = 0; i < choices.Length; i++) {
|
||||
// 枝刈り:部分和が target を超える場合はその選択をスキップする
|
||||
if (total + choices[i] > target) {
|
||||
continue;
|
||||
}
|
||||
// 試行:選択を行い、要素と total を更新する
|
||||
state.Add(choices[i]);
|
||||
// 次の選択へ進む
|
||||
Backtrack(state, target, total + choices[i], choices, res);
|
||||
// バックトラック:選択を取り消し、前の状態に戻す
|
||||
state.RemoveAt(state.Count - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* 部分和 I を解く(重複部分集合を含む) */
|
||||
List<List<int>> SubsetSumINaive(int[] nums, int target) {
|
||||
List<int> state = []; // 状態(部分集合)
|
||||
int total = 0; // 部分和
|
||||
List<List<int>> res = []; // 結果リスト(部分集合のリスト)
|
||||
Backtrack(state, target, total, nums, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [3, 4, 5];
|
||||
int target = 9;
|
||||
List<List<int>> res = SubsetSumINaive(nums, target);
|
||||
Console.WriteLine("入力配列 nums = " + string.Join(", ", nums) + ", target = " + target);
|
||||
Console.WriteLine("和が " + target + " に等しいすべての部分集合 res = ");
|
||||
foreach (var subset in res) {
|
||||
PrintUtil.PrintList(subset);
|
||||
}
|
||||
Console.WriteLine("この方法の出力結果には重複した集合が含まれることに注意してください");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* File: subset_sum_ii.cs
|
||||
* Created Time: 2023-06-25
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class subset_sum_ii {
|
||||
/* バックトラッキング:部分和 II */
|
||||
void Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
|
||||
// 部分集合の和が target に等しければ、解を記録
|
||||
if (target == 0) {
|
||||
res.Add(new List<int>(state));
|
||||
return;
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
// 枝刈り 2: start から走査し、重複する部分集合の生成を避ける
|
||||
// 枝刈り 3: start から走査し、同じ要素の重複選択を避ける
|
||||
for (int i = start; i < choices.Length; i++) {
|
||||
// 枝刈り1:部分集合の和が target を超えたら、直ちにループを終了する
|
||||
// 配列はソート済みで後続要素のほうが大きく、部分集合の和は必ず target を超えるため
|
||||
if (target - choices[i] < 0) {
|
||||
break;
|
||||
}
|
||||
// 枝刈り4:この要素が左隣の要素と等しければ、その探索分岐は重複しているためスキップする
|
||||
if (i > start && choices[i] == choices[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
// 試す:選択を行い、target と start を更新
|
||||
state.Add(choices[i]);
|
||||
// 次の選択へ進む
|
||||
Backtrack(state, target - choices[i], choices, i + 1, res);
|
||||
// バックトラック:選択を取り消し、前の状態に戻す
|
||||
state.RemoveAt(state.Count - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* 部分和 II を解く */
|
||||
List<List<int>> SubsetSumII(int[] nums, int target) {
|
||||
List<int> state = []; // 状態(部分集合)
|
||||
Array.Sort(nums); // nums をソート
|
||||
int start = 0; // 開始点を走査
|
||||
List<List<int>> res = []; // 結果リスト(部分集合のリスト)
|
||||
Backtrack(state, target, nums, start, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [4, 4, 5];
|
||||
int target = 9;
|
||||
List<List<int>> res = SubsetSumII(nums, target);
|
||||
Console.WriteLine("入力配列 nums = " + string.Join(", ", nums) + ", target = " + target);
|
||||
Console.WriteLine("和が " + target + " に等しいすべての部分集合 res = ");
|
||||
foreach (var subset in res) {
|
||||
PrintUtil.PrintList(subset);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user