Dsa
Graphs Bfs
Medium
Traffic Light Timing
You are managing traffic lights at intersections of a city grid. Each intersection can either be open (1) or closed (0). Given a grid representing the intersections and a starting location, determine the maximum time it takes to reach a destination while only passing through open intersections. Each step to an adjacent open intersection takes 1 time unit, and traffic lights delay your entry into an intersection by an extra 2 time units.
Input:
- The first line contains two integers, R (rows) and C (columns) of the grid.
- The next R lines contain C integers each (0 or 1).
- The last line contains the starting position (x y) and destination (x' y').
Output:
- An integer representing the maximum time required to reach the destination or -1 if unreachable.
Example:
Input:
3 3
1 0 1
1 1 1
0 1 1
0 0 2 2
Output:
6
Key concepts
graphsbfstimingtraffic
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now