Harbor Ferry Route Planner
Think of a small harbor challenge where order and timing really matter. In Harbor Ferry Route Planner, you are trying to work toward the right number by following one clear idea.
This problem feels a bit like a maze or map challenge. You need to think about how moves, paths, or links work together. Some places may still connect nicely, while others may be blocked or no longer fit the rule. The answer comes from following those connections carefully.
For example, if the input is n = 4, routes = [[0,1],[1,2],[2,3]], origin = 3, max_transfers = 0, the answer is 1. With no transfers allowed, only the starting dock receives the announcement. Another example is n = 5, routes = [[0,1],[1,2],[2,0],[3,4]], origin = 1, max_transfers = 1, which gives 3. Docks 0, 1, and 2 form a reachable group within one transfer of the origin.
This is a friendly practice problem, but it still rewards careful reading. The key is keeping track of where you can move and which routes still follow the rule.
Example Input & Output
With no transfers allowed, only the starting dock receives the announcement.
Docks 0, 1, and 2 form a reachable group within one transfer of the origin.
The crew may visit docks 0, 1, 2, and 3 before exceeding the transfer cap.
Algorithm Flow

Best Answers
import java.util.*;
class Solution {
public int ferry_route_reach(int n, int[][] routes, int origin, int max_transfers) {
Map<Integer, List<Integer>> graph = new HashMap<>();
for (int i = 0; i < n; i++) graph.put(i, new ArrayList<>());
for (int[] route : routes) {
graph.get(route[0]).add(route[1]);
graph.get(route[1]).add(route[0]);
}
Set<Integer> visited = new HashSet<>();
visited.add(origin);
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{origin, 0});
while (!queue.isEmpty()) {
int[] curr = queue.poll();
int harbor = curr[0], transfers = curr[1];
if (transfers < max_transfers) {
for (int neighbor : graph.get(harbor)) {
if (!visited.contains(neighbor)) {
visited.add(neighbor);
queue.offer(new int[]{neighbor, transfers + 1});
}
}
}
}
return visited.size();
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
