Data Structures & AlgorithmsHardGATE CS

In the grid 11111 / 11112 / 11121 / 11111 (rows top to bottom; 2 = rotten, 1 = fresh, 0 = empty), every minute each rotten orange rots its fresh 4-neighbours. After how many minutes are no fresh oranges left?

  1. A. 6
  2. B. 4
  3. C. 5
  4. D. 7

Correct answer: C. 5

This is a multi-source BFS: put ALL rotten cells in the queue at time 0; the answer is the largest BFS distance to a fresh cell, 5. Running BFS from one rotten orange only overstates it.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic