private static IEnumerable <Point> BreadthSearch(char[,] map, int energy, Dictionary <char, int> price) { var firstEnergyPoint = new EnergyPoint(new Point(0, 0), energy); var visited = new HashSet <Point>(); var queue = new Queue <EnergyPoint>(); queue.Enqueue(firstEnergyPoint); while (queue.Count != 0) { var energyPoint = queue.Dequeue(); if (!InBounds(map, energyPoint.Point) || map[energyPoint.Point.X, energyPoint.Point.Y] == '0' || visited.Contains(energyPoint.Point)) { continue; } visited.Add(energyPoint.Point); yield return(energyPoint.Point); EnqueueNextPoints(map, queue, energyPoint, price); } }
private static void EnqueueNextPoints(char[,] map, Queue <EnergyPoint> queue, EnergyPoint energyEnergyPoint, Dictionary <char, int> price) { for (var dy = -1; dy <= 1; dy++) { for (var dx = -1; dx <= 1; dx++) { if (dx != 0 && dy != 0 || dx == 0 && dy == 0) { continue; } var nextEnergyPoint = new EnergyPoint(new Point(energyEnergyPoint.Point.X + dx, energyEnergyPoint.Point.Y + dy)); if (!InBounds(map, nextEnergyPoint.Point) || map[nextEnergyPoint.Point.X, nextEnergyPoint.Point.Y] == '0') { continue; } nextEnergyPoint.Energy = energyEnergyPoint.Energy - price[map[nextEnergyPoint.Point.X, nextEnergyPoint.Point.Y]]; if (nextEnergyPoint.Energy < 0) { continue; } queue.Enqueue(nextEnergyPoint); } } }