#C13581. Rotting Oranges
Rotting Oranges
Rotting Oranges
You are given a grid of size \(R \times C\) where each cell can have one of three possible values:
- 0 : representing an empty cell,
- 1 : representing a fresh orange, and
- 2 : representing a rotten orange.
Every minute, any fresh orange that is adjacent (up, down, left, or right) to a rotten orange becomes rotten. The process continues until no fresh oranges remain. Your task is to determine the minimum number of minutes that must elapse until there are no fresh oranges left. If this is impossible, output \(-1\).
Input and Output: The program will read the number of rows and columns, followed by the grid, and output a single integer: the required minutes or \(-1\) if it is impossible.
inputFormat
The first line contains two integers \(R\) and \(C\), representing the number of rows and columns, respectively.
Each of the next \(R\) lines contains \(C\) space-separated integers, each being \(0\), \(1\), or \(2\), representing the state of each cell in the grid.
outputFormat
Output a single integer representing the minimum number of minutes that must elapse until no cell has a fresh orange. Output \(-1\) if it is impossible for all oranges to become rotten.
## sample3 3
2 1 1
1 1 0
0 1 1
4