using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WCS.Core; namespace WCS.Core { public class DevicePath { public string From { get; set; } = ""; public string To { get; set; } = ""; List points = new List(); public List Points { get { var list = points.Select(v => Device.Find(v)).ToList(); return list; } } public static DevicePath[] GetPaths(string from, string to) { var all = Device.All.ToDictionary(v => v.Code, v => v.Targets.Select(d => d.Code).ToList()); List> pathList = new List>(); recursion(from, to, all, pathList, new List()); pathList.ForEach(path => path.Remove(from)); var arr = pathList.Select(v => new DevicePath { From = from, To = to, points = v }).ToArray(); return arr; } public static DevicePath? GetPath(string from, string to) { return GetPaths(from, to).OrderBy(v => v.Points.Count).FirstOrDefault(); } static void recursion(string from, string to, Dictionary> all, List> pathList, List points) { if (points.Count > 100) return; points.Add(from); if (from.Contains(to)) { pathList.Add(points); return; } if (!all.ContainsKey(from)) { return; } foreach (var code in all[from]) { if (points.Contains(code)) continue; recursion(code, to, all, pathList, new List(points)); } } } }