You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.7 KiB
93 lines
2.7 KiB
/**
|
|
* 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<TreeNode> state)
|
|
{
|
|
return state.Count != 0 && state[^1].val == 7;
|
|
}
|
|
|
|
/* 记录解 */
|
|
static void recordSolution(List<TreeNode> state, List<List<TreeNode>> res)
|
|
{
|
|
res.Add(new List<TreeNode>(state));
|
|
}
|
|
|
|
/* 判断在当前状态下,该选择是否合法 */
|
|
static bool isValid(List<TreeNode> state, TreeNode choice)
|
|
{
|
|
return choice != null && choice.val != 3;
|
|
}
|
|
|
|
/* 更新状态 */
|
|
static void makeChoice(List<TreeNode> state, TreeNode choice)
|
|
{
|
|
state.Add(choice);
|
|
}
|
|
|
|
/* 恢复状态 */
|
|
static void undoChoice(List<TreeNode> state, TreeNode choice)
|
|
{
|
|
state.RemoveAt(state.Count - 1);
|
|
}
|
|
|
|
/* 回溯算法 */
|
|
static void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res)
|
|
{
|
|
// 检查是否为解
|
|
if (isSolution(state))
|
|
{
|
|
// 记录解
|
|
recordSolution(state, res);
|
|
return;
|
|
}
|
|
// 遍历所有选择
|
|
foreach (TreeNode choice in choices)
|
|
{
|
|
// 剪枝:检查选择是否合法
|
|
if (isValid(state, choice))
|
|
{
|
|
// 尝试:做出选择,更新状态
|
|
makeChoice(state, choice);
|
|
List<TreeNode> nextChoices = new List<TreeNode>() { 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<List<TreeNode>> res = new List<List<TreeNode>>();
|
|
List<TreeNode> choices = new List<TreeNode>() { root };
|
|
backtrack(new List<TreeNode>(), choices, res);
|
|
|
|
Console.WriteLine("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点");
|
|
foreach (List<TreeNode> path in res)
|
|
{
|
|
List<int> vals = new List<int>();
|
|
foreach (TreeNode node in path)
|
|
{
|
|
vals.Add(node.val);
|
|
}
|
|
Console.WriteLine(string.Join(" ", vals));
|
|
}
|
|
}
|
|
} |