Seaport Ferry Gate Control
The harbor operations team manages a network of ferry gates (0 to n-1). During high tide, some docks are Closed for safety, and vessels cannot pass through or land there. Your mission in Seaport Ferry Gate Control is to count how many unique gates remain accessible from a specific start gate without crossing any closed piers.
The "secret sauce" here is Graph Traversal with Barriers. Use BFS or DFS to explore every route from the origin, but immediately skip any path that enters a closed gate. If the starting gate itself is closed, the answer is zero. This simple traversal helps dispatchers adjust staffing based on which parts of the bay are currently connected. It’s a practical connectivity problem that teaches you how to model and traverse relationships under dynamic constraints. Precision is key to keeping the harbor running safely!
When the task involves connectivity or route cost, build adjacency carefully and guard against revisiting stale states. Use a visited or best-distance structure to avoid repeated work, and ensure unreachable scenarios return the required fallback value instead of partial traversal results.
Examples
Starting at gate 4 allows access to gates 2 and 3 even though gate 1 is closed.
Gate 3 has no connecting routes, so only the origin counts toward the total.
Gates 1, 0, and 2 stay connected, while gates beyond the closed gate 3 are unreachable.
Algorithm Flow

Best Answers
import java.util.*;
class Solution {
public int ferry_gate_control(int n, int[][] routes, int start, int[] closed_gates) {
Set<Integer> closed = new HashSet<>();
for (int g : closed_gates) closed.add(g);
if (closed.contains(start)) return 0;
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) adj.add(new ArrayList<>());
for (int[] route : routes) {
int u = route[0], v = route[1];
if (!closed.contains(u) && !closed.contains(v)) {
adj.get(u).add(v);
adj.get(v).add(u);
}
}
Set<Integer> visited = new HashSet<>();
visited.add(start);
Queue<Integer> queue = new LinkedList<>();
queue.add(start);
while (!queue.isEmpty()) {
int curr = queue.poll();
for (int neighbor : adj.get(curr)) {
if (!visited.contains(neighbor)) {
visited.add(neighbor);
queue.add(neighbor);
}
}
}
return visited.size();
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this problem.
