Wildlife Bridge Network Scan
In Wildlife Bridge Network Scan, you are given a network of numbered points, a starting point, and a list of blocked points. Your task is to count how many points can still be reached from the start while respecting the blocked list.
The input includes n for the number of points, paths for the connections, start for where the scan begins, and closed_bridges for the locations that should not be entered. The result is not the actual set of visited points. It is just the count.
For example, one of the test cases starts from point 0 in a small network and blocks point 2. In that case, the scan can still cover 0 and 1, so the answer is 2. Another test starts from point 4 in a network with no blocked points and reaches four positions total, so the answer is 4.
So the task is to build the network, avoid blocked positions, explore from the start, and return how many points are reachable under those rules.
Example Input & Output
Point 2 is blocked, so the scan reaches only 0 and 1 in that component.
With no blocked points, the scan covers the whole connected component containing 4.
A single open node is reachable from itself, so the count is 1.
Algorithm Flow

Solution Approach
A good way to solve this problem is to treat the input as a graph reachability task and run a normal graph traversal from the start node. The two standard options are depth-first search (DFS) or breadth-first search (BFS). Either one works because the problem only asks for how many nodes are reachable, not for a shortest path.
The first step is to build an adjacency list from the paths input. That gives you a quick way to find every neighbor of a node during traversal. After that, place the blocked points into a set so you can test membership in constant time. If the start node itself is blocked, then the reachable count should be 0 or 1 depending on the exact export rule, but the shown tests all keep the start usable, so the main traversal logic is still straightforward.
In JavaScript, the structure can look like this:
This uses DFS with an explicit stack, but a queue-based BFS would be just as valid. The important parts are: never revisit a node already seen, never enter a blocked node, and keep expanding through the graph until no more legal moves remain.
This matches the intent of the tests and examples. When no points are blocked, the traversal covers the whole connected component of the start node. When some points are blocked, they cut off part of the network and reduce the reachable count.
The time complexity is O(n + m), where m is the number of paths, because each node and edge is processed at most a constant number of times. The extra space is also O(n + m) for the graph, visited set, and traversal structure.
So the full strategy is: build the graph, store blocked nodes in a set, traverse from the start while skipping blocked and already seen nodes, and return the number of visited nodes.
Best Answers
import java.util.*;
class Solution {
public int wildlife_bridge_reach(int n, int[][] paths, int start, int[] closed_bridges) {
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) adj.add(new ArrayList<>());
Set<Integer> closed = new HashSet<>();
for (int b : closed_bridges) closed.add(b);
for (int[] path : paths) {
if (path[0] < n && path[1] < n) {
adj.get(path[0]).add(path[1]);
adj.get(path[1]).add(path[0]);
}
}
if (closed.contains(start)) return 0;
Set<Integer> visited = new HashSet<>();
Queue<Integer> queue = new LinkedList<>();
visited.add(start);
queue.add(start);
while (!queue.isEmpty()) {
int curr = queue.poll();
for (int neighbor : adj.get(curr)) {
if (!visited.contains(neighbor) && !closed.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 Challenge.
