Editor

Wildfire

#118 · Graphs · Advanced · 110 XP · about 18 min

A forest is a list of strings of equal length: 'F' is a tree on fire, 'T' is a tree and '.' is bare ground. Every minute, each burning tree sets fire to the trees directly above, below, left and right of it. Fire never crosses bare ground and never spreads diagonally.

Write burn_time(forest) that returns the number of minutes until no unburned tree is left. If some trees can never catch fire, return -1. If no unburned tree is left at the start, the answer is 0.

Examples

burn_time(['FT.', 'TT.', '.TT'])  # returns 4
burn_time(['FT.T'])  # returns -1
burn_time(['TTTTT', 'F...F'])  # returns 3

In the first forest the bottom right tree is the last to catch fire, after 4 minutes. In the second, bare ground protects the tree on the right. In the third, two fires burn along the top row toward each other and meet in the middle after 3 minutes.

Constraints

  • 1 to 50 rows, each with 1 to 50 characters.
  • Any number of trees can be burning at the start, including none.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.