Path.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. namespace WCS.Core;
  2. public class DevicePath
  3. {
  4. private List<string> points = new();
  5. public string From { get; set; } = "";
  6. public string To { get; set; } = "";
  7. public List<Device> Points
  8. {
  9. get
  10. {
  11. var list = points.Select(v => Device.Find(v)).ToList();
  12. return list;
  13. }
  14. }
  15. public static DevicePath[] GetPaths(string from, string to)
  16. {
  17. var all = Device.All.ToDictionary(v => v.Code, v => v.Targets.Select(d => d.Code).ToList());
  18. var pathList = new List<List<string>>();
  19. recursion(from, to, all, pathList, new List<string>());
  20. pathList.ForEach(path => path.Remove(from));
  21. var arr = pathList.Select(v => new DevicePath { From = from, To = to, points = v }).ToArray();
  22. return arr;
  23. }
  24. public static DevicePath? GetPath(string from, string to)
  25. {
  26. return GetPaths(from, to).OrderBy(v => v.Points.Count).FirstOrDefault();
  27. }
  28. private static void recursion(string from, string to, Dictionary<string, List<string>> all,
  29. List<List<string>> pathList, List<string> points)
  30. {
  31. if (points.Count > 100)
  32. return;
  33. points.Add(from);
  34. if (from.Contains(to))
  35. {
  36. pathList.Add(points);
  37. return;
  38. }
  39. if (!all.ContainsKey(from)) return;
  40. foreach (var code in all[from])
  41. {
  42. if (points.Contains(code))
  43. continue;
  44. recursion(code, to, all, pathList, new List<string>(points));
  45. }
  46. }
  47. }