BudiBadu Logo

Mountain Cabin Signal Range

String Easy 2 views
Like30

Mountain cabins are linked by suspension bridges. Your mission in Mountain Cabin Signal Range is to find how many unique cabins can receive a signal from a starting origin without crossing any Blocked Bridges. You treat the cabins as nodes and the bridges as edges in a network map.

The "secret sauce" here is Graph Traversal with Edge Filters. Using BFS or DFS, explore every possible path from the start cabin while immediately skipping any bridge in the blocked_bridges list. Duplicate or reversed entries in the data shouldn't change the reachable count. Every accessible cabin is counted once, and the starting point always counts as a recipient. This audit tells the coordinator exactly which parts of the mountain are currently in sync! It’s an essential skill for building logistics or disaster relief routing software under dynamic constraints.

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 = 6, bridges = [[0,1],[1,2],[2,3],[3,4],[4,5]], start = 0, blocked_bridges = [[2,3]]
Output
3
Explanation

Cabins 0, 1, and 2 receive the alert before reaching the blocked bridge between cabins 2 and 3.

Example 2
Input
n = 4, bridges = [[0,1],[1,2]], start = 3, blocked_bridges = [[2,1]]
Output
1
Explanation

Cabin 3 stands alone, so only the origin is counted.

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

With no blocked bridges, the signal reaches every cabin in the connected component.

Algorithm Flow

Recommendation Algorithm Flow for Mountain Cabin Signal Range - Budibadu
Recommendation Algorithm Flow for Mountain Cabin Signal Range - Budibadu

Best Answers

java
class Solution {
    public double calculate_min_range(int[][] cabins) {
        int n = cabins.length;
        if (n <= 1) return 0.0;
        double[] minD = new double[n];
        Arrays.fill(minD, Double.MAX_VALUE);
        minD[0] = 0.0;
        boolean[] visited = new boolean[n];
        double res = 0.0;
        for (int i = 0; i < n; i++) {
            int u = -1;
            for (int j = 0; j < n; j++) {
                if (!visited[j] && (u == -1 || minD[j] < minD[u])) u = j;
            }
            visited[u] = true;
            res = Math.max(res, minD[u]);
            for (int v = 0; v < n; v++) {
                if (!visited[v]) {
                    double d = Math.sqrt(Math.pow(cabins[u][0] - cabins[v][0], 2) + Math.pow(cabins[u][1] - cabins[v][1], 2));
                    minD[v] = Math.min(minD[v], d);
                }
            }
        }
        return res;
    }
}