-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllPossibilitiesRouter.cs
More file actions
45 lines (35 loc) · 1.11 KB
/
AllPossibilitiesRouter.cs
File metadata and controls
45 lines (35 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Collections.Generic;
using System.Linq;
namespace RouterSample
{
public class AllPossibilitiesRouter : IRouter
{
List<Node> visiting = new List<Node>();
public List<List<string>> GetRoutes(Node node, string target)
{
if (node.Value == target)
{
visiting.Remove(node);
return new List<List<string>> { new List<string> { node.Value } };
}
if (node.Nodes.Count < 1 || visiting.Any(e => e == node))
return null;
var result = new List<List<string>>();
visiting.Add(node);
foreach (var item in node.Nodes)
{
var routes = GetRoutes(item, target);
if (routes != null)
{
foreach (var route in routes)
route.Insert(0, node.Value);
result.AddRange(routes);
}
}
visiting.Remove(node);
if (result.Count < 1)
return null;
return result;
}
}
}