private void EvaluateOpenSteps(MazeStep mazeStep, Queue <MazeStep> queue, List <Coordinate> allPreviousCoordinates) { if (HaveNotAlreadyBeenToCoordinate(mazeStep.Coord, allPreviousCoordinates) == true) { allPreviousCoordinates.Add(mazeStep.Coord); Coordinate north = new Coordinate(mazeStep.Coord.X, mazeStep.Coord.Y - 1); if (IsOpenSpace(north) == true && HaveNotAlreadyBeenToCoordinate(north, allPreviousCoordinates) == true) { MazeStep northStep = new MazeStep(north, mazeStep.Step + 1, null); queue.Enqueue(northStep); } Coordinate east = new Coordinate(mazeStep.Coord.X + 1, mazeStep.Coord.Y); if (IsOpenSpace(east) == true && HaveNotAlreadyBeenToCoordinate(east, allPreviousCoordinates) == true) { MazeStep eastStep = new MazeStep(east, mazeStep.Step + 1, null); queue.Enqueue(eastStep); } Coordinate south = new Coordinate(mazeStep.Coord.X, mazeStep.Coord.Y + 1); if (IsOpenSpace(south) == true && HaveNotAlreadyBeenToCoordinate(south, allPreviousCoordinates) == true) { MazeStep southStep = new MazeStep(south, mazeStep.Step + 1, null); queue.Enqueue(southStep); } Coordinate west = new Coordinate(mazeStep.Coord.X - 1, mazeStep.Coord.Y); if (IsOpenSpace(west) == true && HaveNotAlreadyBeenToCoordinate(west, allPreviousCoordinates) == true) { MazeStep westStep = new MazeStep(west, mazeStep.Step + 1, null); queue.Enqueue(westStep); } } }
public int NumberOfStepsToTarget(Coordinate startCoord, Coordinate targetCoord) { Queue <MazeStep> queue = new Queue <MazeStep>(); MazeStep start = new MazeStep(startCoord, 0, null); List <Coordinate> allPreviousCoordinates = new List <Coordinate>(); queue.Enqueue(start); // Keep iterating over queue until we get a result do { MazeStep currentState = queue.Dequeue(); // If hits are target - return if (currentState.ReachedDesintation(targetCoord)) { return(currentState.Step); } // Figure out other valid paths and add them to queue while iterating step EvaluateOpenSteps(currentState, queue, allPreviousCoordinates); Debug.WriteLine($"prevCoord: {allPreviousCoordinates.Count} queue: {queue.Count}."); } while (queue.Any()); return(0); }
public int MaximumLocationsInNumberOfSteps(Coordinate startCoord, int favNumber, int maxSteps) { MazeStep start = new MazeStep(startCoord, 0, new List <Coordinate>()); _queue.Add(start); List <Coordinate> allPlacesVisited = new List <Coordinate>(); // Keep iterating over queue until we get through all posibilities do { MazeStep mazeStep = _queue[0]; _queue.RemoveAt(0); // Capture places visited allPlacesVisited.Add(mazeStep.Coord); // If exceed maxSteps do not go further if (mazeStep.Step < maxSteps) { // Figure out other valid paths and add them to queue while iterating step EvaluateOpenSteps(mazeStep, favNumber); } } while (_queue.Any()); // Trim down the locations for uniqueness var uniqueList = new HashSet <Coordinate>(allPlacesVisited, new CoordinateEqualityComparer()); return(uniqueList.Count); }
private void EvaluateOpenSteps(MazeStep mazeStep, int favNumber) { Coordinate north = new Coordinate(mazeStep.Coord.X, mazeStep.Coord.Y - 1); if (IsOpenSpace(north, favNumber) && mazeStep.HaveAlreadyBeenToCoordinate(north) == false) { List <Coordinate> previous = mazeStep.ClonePreviousCoords(); previous.Add(mazeStep.Coord); MazeStep northStep = new MazeStep(north, mazeStep.Step + 1, previous); _queue.Add(northStep); } Coordinate east = new Coordinate(mazeStep.Coord.X + 1, mazeStep.Coord.Y); if (IsOpenSpace(east, favNumber) && mazeStep.HaveAlreadyBeenToCoordinate(east) == false) { List <Coordinate> previous = mazeStep.ClonePreviousCoords(); previous.Add(mazeStep.Coord); MazeStep eastStep = new MazeStep(east, mazeStep.Step + 1, previous); _queue.Add(eastStep); } Coordinate south = new Coordinate(mazeStep.Coord.X, mazeStep.Coord.Y + 1); if (IsOpenSpace(south, favNumber) && mazeStep.HaveAlreadyBeenToCoordinate(south) == false) { List <Coordinate> previous = mazeStep.ClonePreviousCoords(); previous.Add(mazeStep.Coord); MazeStep southStep = new MazeStep(south, mazeStep.Step + 1, previous); _queue.Add(southStep); } Coordinate west = new Coordinate(mazeStep.Coord.X - 1, mazeStep.Coord.Y); if (IsOpenSpace(west, favNumber) && mazeStep.HaveAlreadyBeenToCoordinate(west) == false) { List <Coordinate> previous = mazeStep.ClonePreviousCoords(); previous.Add(mazeStep.Coord); MazeStep westStep = new MazeStep(west, mazeStep.Step + 1, previous); _queue.Add(westStep); } }
public int MinimumNumberOfStepsToCoordinates(Coordinate startCoord, int favNumber, Coordinate targetCoord) { MazeStep start = new MazeStep(startCoord, 0, new List <Coordinate>()); _queue.Add(start); // Keep iterating over queue until we get a result do { MazeStep mazeStep = _queue[0]; _queue.RemoveAt(0); // If hits are target - return if (mazeStep.ReachedDesintation(targetCoord)) { return(mazeStep.Step); } // Figure out other valid paths and add them to queue while iterating step EvaluateOpenSteps(mazeStep, favNumber); } while (_queue.Any()); return(0); }