BudiBadu Logo

Seaport Ferry Gate Control

Array Easy 7 views
Like7

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

Example 1
Input
n = 5, routes = [[0,2],[2,4],[1,2],[3,4],[0,1]], start = 4, closed_gates = [1]
Output
3
Explanation

Starting at gate 4 allows access to gates 2 and 3 even though gate 1 is closed.

Example 2
Input
n = 4, routes = [[0,1],[1,2]], start = 3, closed_gates = []
Output
1
Explanation

Gate 3 has no connecting routes, so only the origin counts toward the total.

Example 3
Input
n = 6, routes = [[0,1],[1,2],[2,3],[3,4],[4,5]], start = 1, closed_gates = [3]
Output
3
Explanation

Gates 1, 0, and 2 stay connected, while gates beyond the closed gate 3 are unreachable.

Algorithm Flow

Recommendation Algorithm Flow for Seaport Ferry Gate Control - Budibadu
Recommendation Algorithm Flow for Seaport Ferry Gate Control - Budibadu

Best Answers

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