Rotten Oranges
Given a matrix of dimension r*c where each cell in the matrix can have values 0, 1 or 2 which has the following meaning:
- 0 : Empty cell
- 1 : Cells have fresh oranges
- 2 : Cells have rotten oranges
So, we have to determine what is the minimum time required to all oranges. A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time. If it is impossible to rot every orange then simply return -1.
Example:
Input:
2
3 5
2 1 0 2 1 1 0 1 2 1 1 0 0 2 1
Output:
2
Algorithm:
To implement this question we use BFS and a queue data structure.
C++ Implementation for Rotten Oranges problem
Output
need an explanation for this answer? contact us directly to get an explanation for this answer