Dsa
Graphs Bfs
Medium
Zombie Infection Spread
A zombie outbreak has occurred in a town represented as a grid. Each cell in the grid can either be empty, contain a human, or contain a zombie. The zombies spread their infection to the adjacent cells (up, down, left, right) each time unit. Your task is to determine how many time units it takes for all humans to be infected or confirm that not all can be infected.
Input:
- The first line contains two integers, R (rows) and C (columns) of the grid.
- The next R lines contain C characters each, consisting of:
- 'H' for human
- 'Z' for zombie
- '.' for an empty cell
Output:
- An integer representing the time units required for all humans to be infected, or -1 if it is impossible.
Example:
Input:
3 3
H..
Z.H
...
Output:
1
Key concepts
graphsbfsinfectionspread
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