private static List <Road> BFS(Road from, Road to) { Queue <Road> queue = new Queue <Road>(); HashSet <Road> explored = new HashSet <Road>(); Dictionary <Road, Road> parents = new Dictionary <Road, Road>(); queue.Enqueue(from); while (queue.Count > 0) { Road current = queue.Dequeue(); if (current == to) { break; } RoadArray links = GetLinks(current); for (int i = 0; i < links.Length; i++) { Road link = links[i]; if (!explored.Contains(link)) { explored.Add(link); parents.Add(link, current); queue.Enqueue(link); } } if (queue.Count == 0) { return(null); } } List <Road> path = new List <Road>(); Road curr = to; try { while (curr != from) { path.Add(curr); curr = parents[curr]; } path.Add(from); path.Reverse(); // ? return(path); } catch (KeyNotFoundException) { return(null); } }
private static RoadArray GetLinks(Road current) { RoadArray array = new RoadArray(4); if (current.north != null) { array.Add(current.north); } if (current.south != null) { array.Add(current.south); } if (current.east != null) { array.Add(current.east); } if (current.west != null) { array.Add(current.west); } return(array); }