You've been hired by a company to help simulate the spread of diseases in orchards to…
- User
You've been hired by a company to help simulate the spread of diseases in orchards to devise containment strategies.
Given a 2D grid orchard where each cell is represented by a character: "-" (empty), "T" (healthy tree), or "R" (rotten tree). Each day, rot spreads to adjacent cells (up, down, left, right) from any rotten tree. Healthy trees adjacent to rotten trees will become rotten.
Given an integer days, simulate the spread of rot over the orchard for the specified number of days. Return the state of the orchard as a 2D grid of strings after the simulation.
Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(days * rows * cols) will fit within the execution time limit.
Example
For
orchard = [ ["T", "R", "-", "-"], ["-", "T", "T", "R"], ["T", "T", "-", "R"], ["-", "-", "R", "-"] ]
and days = 2, the output should be
solution(orchard, days) = [ ["R", "R", "-", "-"], ["-", "R", "R", "R"], ["T", "R", "-", "R"], ["-", "-", "R", "-"] ]
give in cpp
- Assistant