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); } } }