private static List<Position> GetPossibleDirections(Position position, Labyrinth labyrinth) { var possibleDirections = new List<Position>(); var left = position.Left; var right = position.Right; var down = position.Down; var up = position.Up; if (labyrinth.CanMoveTo(left)) { possibleDirections.Add(left); } if (labyrinth.CanMoveTo(right)) { possibleDirections.Add(right); } if (labyrinth.CanMoveTo(down)) { possibleDirections.Add(down); } if (labyrinth.CanMoveTo(up)) { possibleDirections.Add(up); } return possibleDirections; }
private static void GetAllPaths(Position position, Position target, Labyrinth labyrinth) { labyrinth.Visit(position); if (position == target) { foundPaths.Add(labyrinth); return; } var possibleDirections = new List<Position> { position.Left, position.Right, position.Down, position.Up }; foreach (var direction in possibleDirections) { if (labyrinth.CanMoveTo(direction)) { var newLabyrinth = new Labyrinth(labyrinth.Matrix, new HashSet<Position>(labyrinth.Visited)); GetAllPaths(direction, target, newLabyrinth); } } }
private static void PathAvailable(Position position, Position target, Labyrinth labyrinth) { if (position == target) { pathFound = true; } labyrinth.Visit(position); var possibleDirections = GetPossibleDirections(position, labyrinth); foreach (var direction in possibleDirections) { if (labyrinth.CanMoveTo(direction) && !pathFound) { PathAvailable(direction, target, labyrinth); } } }