Path.cs 1.9 KB

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