diff --git a/codes/csharp/chapter_backtracking/backtrack_find_constrained_paths.cs b/codes/csharp/chapter_backtracking/backtrack_find_constrained_paths.cs new file mode 100644 index 000000000..02389eba8 --- /dev/null +++ b/codes/csharp/chapter_backtracking/backtrack_find_constrained_paths.cs @@ -0,0 +1,93 @@ +/** + * File: backtrack_find_constrained_paths.cs + * Created Time: 2023-04-17 + * Author: hpstory (hpstory1024@163.com) + */ + +using hello_algo.include; +using NUnit.Framework; + +namespace hello_algo.chapter_backtracking; + +public class backtrack_find_constrained_paths +{ + /* 判断当前状态是否为解 */ + static bool isSolution(List state) + { + return state.Count != 0 && state[^1].val == 7; + } + + /* 记录解 */ + static void recordSolution(List state, List> res) + { + res.Add(new List(state)); + } + + /* 判断在当前状态下,该选择是否合法 */ + static bool isValid(List state, TreeNode choice) + { + return choice != null && choice.val != 3; + } + + /* 更新状态 */ + static void makeChoice(List state, TreeNode choice) + { + state.Add(choice); + } + + /* 恢复状态 */ + static void undoChoice(List state, TreeNode choice) + { + state.RemoveAt(state.Count - 1); + } + + /* 回溯算法 */ + static void backtrack(List state, List choices, List> res) + { + // 检查是否为解 + if (isSolution(state)) + { + // 记录解 + recordSolution(state, res); + return; + } + // 遍历所有选择 + foreach (TreeNode choice in choices) + { + // 剪枝:检查选择是否合法 + if (isValid(state, choice)) + { + // 尝试:做出选择,更新状态 + makeChoice(state, choice); + List nextChoices = new List() { choice.left, choice.right }; + backtrack(state, nextChoices, res); + // 回退:撤销选择,恢复到之前的状态 + undoChoice(state, choice); + } + } + } + + [Test] + public void Test() + { + TreeNode root = TreeNode.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 }); + Console.WriteLine("\n初始化二叉树"); + PrintUtil.PrintTree(root); + + // 回溯算法 + List> res = new List>(); + List choices = new List() { root }; + backtrack(new List(), choices, res); + + Console.WriteLine("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点"); + foreach (List path in res) + { + List vals = new List(); + foreach (TreeNode node in path) + { + vals.Add(node.val); + } + Console.WriteLine(string.Join(" ", vals)); + } + } +} \ No newline at end of file diff --git a/codes/csharp/chapter_backtracking/preorder_find_constrained_paths.cs b/codes/csharp/chapter_backtracking/preorder_find_constrained_paths.cs new file mode 100644 index 000000000..0a545ef95 --- /dev/null +++ b/codes/csharp/chapter_backtracking/preorder_find_constrained_paths.cs @@ -0,0 +1,61 @@ +/** + * File: preorder_find_constrained_paths.cs + * Created Time: 2023-04-17 + * Author: hpstory (hpstory1024@163.com) + */ + +using hello_algo.include; +using NUnit.Framework; + +namespace hello_algo.chapter_backtracking; + +public class preorder_find_constrained_paths +{ + static List path; + static List> res; + + /* 前序遍历 */ + static void preOrder(TreeNode root) + { + // 剪枝 + if (root == null || root.val == 3) + { + return; + } + // 尝试 + path.Add(root); + if (root.val == 7) + { + // 记录解 + res.Add(new List(path)); + } + preOrder(root.left); + preOrder(root.right); + // 回退 + path.RemoveAt(path.Count - 1); + } + + [Test] + public void Test() + { + TreeNode root = TreeNode.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 }); + Console.WriteLine("\n初始化二叉树"); + PrintUtil.PrintTree(root); + + // 前序遍历 + path = new List(); + res = new List>(); + preOrder(root); + + Console.WriteLine("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点"); + foreach (List path in res) + { + List vals = new List(); + foreach (TreeNode node in path) + { + vals.Add(node.val); + } + Console.WriteLine(string.Join(" ", vals)); + } + } +} \ No newline at end of file diff --git a/codes/csharp/chapter_backtracking/preorder_find_nodes.cs b/codes/csharp/chapter_backtracking/preorder_find_nodes.cs new file mode 100644 index 000000000..76c383cc8 --- /dev/null +++ b/codes/csharp/chapter_backtracking/preorder_find_nodes.cs @@ -0,0 +1,51 @@ +/** + * File: preorder_find_nodes.cs + * Created Time: 2023-04-17 + * Author: hpstory (hpstory1024@163.com) + */ + +using hello_algo.include; +using NUnit.Framework; + +namespace hello_algo.chapter_backtracking; + +public class preorder_find_nodes +{ + static List res; + + /* 前序遍历 */ + static 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.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 }); + Console.WriteLine("\n初始化二叉树"); + PrintUtil.PrintTree(root); + + // 前序遍历 + res = new List(); + preOrder(root); + + Console.WriteLine("\n输出所有值为 7 的节点"); + List vals = new List(); + foreach (TreeNode node in res) + { + vals.Add(node.val); + } + Console.WriteLine(string.Join(" ", vals)); + } +} \ No newline at end of file diff --git a/codes/csharp/chapter_backtracking/preorder_find_paths.cs b/codes/csharp/chapter_backtracking/preorder_find_paths.cs new file mode 100644 index 000000000..ae6503a9b --- /dev/null +++ b/codes/csharp/chapter_backtracking/preorder_find_paths.cs @@ -0,0 +1,60 @@ +/** + * File: preorder_find_paths.cs + * Created Time: 2023-04-17 + * Author: hpstory (hpstory1024@163.com) + */ + +using hello_algo.include; +using NUnit.Framework; + +namespace hello_algo.chapter_backtracking; + +public class preorder_find_paths +{ + static List path; + static List> res; + + /* 前序遍历 */ + static void preOrder(TreeNode root) + { + if (root == null) + { + return; + } + // 尝试 + path.Add(root); + if (root.val == 7) + { + // 记录解 + res.Add(new List(path)); + } + preOrder(root.left); + preOrder(root.right); + // 回退 + path.RemoveAt(path.Count - 1); + } + + [Test] + public void Test() + { + TreeNode root = TreeNode.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 }); + Console.WriteLine("\n初始化二叉树"); + PrintUtil.PrintTree(root); + + // 前序遍历 + path = new List(); + res = new List>(); + preOrder(root); + + Console.WriteLine("\n输出所有根节点到节点 7 的路径"); + foreach (List path in res) + { + List vals = new List(); + foreach (TreeNode node in path) + { + vals.Add(node.val); + } + Console.WriteLine(string.Join(" ", vals)); + } + } +} \ No newline at end of file