Simulate Orchard Rot Spread
Problem statement
An orchard is represented by a rectangular array of strings. Each character is - for an empty cell, T for a healthy tree, or R for a rotten tree.
Each day, every tree that was rotten at the start of that day infects its healthy neighbors directly above, below, left, and right. The changes occur simultaneously: a tree that becomes rotten today starts spreading rot on the next day. Empty cells never change and cannot transmit rot.
Return the orchard after exactly days days, as an array of strings in the same row order. A solution with time complexity O(days * rows * columns) is sufficient; a faster solution is also valid.
For this exercise, assume days may be zero, in which case the orchard is unchanged.
Function
simulateOrchard(orchard: String[], days: int) → String[]Examples
Example 1
orchard = ["RTT","T-T"]days = 1return = ["RRT","R-T"]Only the trees immediately right of and below the initial rotten tree are infected on day one. The next tree to the right must wait until day two.
Example 2
orchard = ["RTT","T-T"]days = 3return = ["RRR","R-R"]The upper-right tree is infected on day two. It infects the lower-right tree on day three. The empty cell remains unchanged.
Example 3
orchard = ["T-R"]days = 0return = ["T-R"]No day is simulated, so no cell changes.
Constraints
- For this exercise, assume
1 <= orchard.length <= 100. - For this exercise, assume all rows have the same length between
1and100. - Each cell is
-,T, orR. - For this exercise, assume
0 <= days <= 100.