Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find number of days it will take to burn all trees in python
Suppose we have a 2D matrix that represents a forest where there are three types of cells: 0 (empty cell), 1 (tree cell), and 2 (tree on fire cell). Every day, a tree catches fire when there is an adjacent (top, down, left, right, not diagonal) tree on fire. We have to find the number of days it would take for every tree to be on fire. If that is not possible, return -1.
Problem Example
If the input matrix is:
| 1 | 2 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Then the output will be 4 days.
Algorithm Steps
To solve this problem, we use a BFS (Breadth-First Search) approach:
- Initialize a counter for days and find all initially burning trees
- For each day, spread fire to adjacent trees
- Continue until no more trees can catch fire
- Return the number of days if all trees are burned, otherwise return -1
Implementation
class Solution:
def solve(self, matrix):
days = 0
burning_trees = []
# Find all initially burning trees
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if matrix[i][j] == 2:
burning_trees.append((i, j))
# Spread fire day by day
while burning_trees:
new_fires = []
# Check all currently burning trees
for i, j in burning_trees:
# Check all 4 adjacent directions
for x, y in [(i + 1, j), (i, j + 1), (i - 1, j), (i, j - 1)]:
# If adjacent cell is a tree, set it on fire
if 0 <= x < len(matrix) and 0 <= y < len(matrix[0]) and matrix[x][y] == 1:
matrix[x][y] = 2
new_fires.append((x, y))
# Update burning trees for next day
burning_trees = new_fires
days += 1 if burning_trees else 0
# Check if any trees remain unburned
remaining_trees = sum(matrix[i][j] == 1 for i in range(len(matrix)) for j in range(len(matrix[0])))
return days if remaining_trees == 0 else -1
# Test the solution
ob = Solution()
matrix = [
[1, 2, 1],
[1, 0, 1],
[1, 1, 1]
]
result = ob.solve(matrix)
print(f"Days to burn all trees: {result}")
Days to burn all trees: 4
How It Works
The algorithm simulates fire spreading day by day:
- Day 0: Fire starts at position (0,1)
- Day 1: Fire spreads to adjacent trees at (0,0) and (0,2)
- Day 2: Fire spreads to (1,2)
- Day 3: Fire spreads to (2,2)
- Day 4: Fire spreads to (2,1) and (2,0)
Edge Cases
# Case 1: Isolated trees that cannot burn
matrix1 = [
[1, 0, 2],
[0, 0, 0],
[0, 0, 1]
]
# Case 2: All trees already burning
matrix2 = [
[2, 2],
[2, 0]
]
ob = Solution()
print(f"Isolated trees case: {ob.solve(matrix1)}")
print(f"All burning case: {ob.solve(matrix2)}")
Isolated trees case: -1 All burning case: 0
Time Complexity
The time complexity is O(m × n) where m and n are the dimensions of the matrix. In the worst case, we visit each cell once when the fire spreads across the entire forest.
Conclusion
This problem uses BFS to simulate fire spreading through a forest. The key insight is to process all burning trees simultaneously for each day and track newly ignited trees for the next iteration.
