Let 'M' = 3, 'N' = 3, and 'A' =
2 1 1
1 1 0
0 1 1
Initially, the rotten tomato is at (0, 0).
After 1 minute, the fresh tomatoes at (0, 1) and (1, 0) become rotten.
The matrix becomes:
2 2 1
2 1 0
0 1 1
After 2 minutes, the tomato at (1, 1) becomes rotten (from (0,1) or (1,0)).
The matrix becomes:
2 2 1
2 2 0
0 1 1
After 3 minutes, the tomatoes at (0, 2) and (2, 1) become rotten.
The matrix becomes:
2 2 2
2 2 0
0 2 1
After 4 minutes, the tomato at (2, 2) becomes rotten.
The matrix becomes:
2 2 2
2 2 0
0 2 2
All fresh tomatoes have become rotten. The minimum time taken is 4.
The first line contains two integers, 'M' and 'N'.
The next 'M' lines contain 'N' integers each, representing the matrix 'A'.
Return the minimum time required for all fresh tomatoes to become rotten. Return -1 if this is not possible.
You don’t need to print anything. Just implement the given function.
1 <= 'M', 'N' <= 100
0 <= 'A[i][j]' <= 2
Time Limit: 1 sec
Approach:
Algorithm: