Simulate Bomb Explosions and Falling Boxes Problem
#4096
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 4096.
You are given a 2D character array grid of size m x n, where each cell contains one of the following symbols:
'#': a box.'-': an empty cell.'*': a bomb.
Each second, the following events occur in order:
- Explosions: A bomb explodes if it is orthogonally adjacent to at least one box (shares a side with it). An exploding bomb destroys all boxes in its 8 neighboring cells, including diagonally adjacent cells, turning them into empty cells. All explosions happen simultaneously, based on the grid at the beginning of that second. Empty cells and bombs remain unchanged.
- Gravity: After the explosions, all non-static boxes simultaneously fall down by exactly one row. A box is static if it is in the bottommost row, immediately above a bomb, or immediately above another static box.
Bombs never move or disappear, and the same bomb may explode multiple times. Boxes above non-static boxes can fall together with them.
Return the final grid after 10^100 seconds.
Example 1:
Input: grid = [["#","#","#"],["-","-","*"],["-","-","-"]]
Output: [["-","-","-"],["-","-","*"],["#","-","-"]]
Explanation:
Let (r, c) represent the cell in the r^th row from the top and c^th column from the left. For example, (0, 0) denotes the top-left most cell.
The 1^st second:
- Since the bomb at (1, 2) is orthogonally adjacent to the box at (0, 2), the boxes at (0, 1) and (0, 2) become empty cells.
- The box at (0, 0) falls to (1, 0).
The 2^nd second:
- The bomb at (1, 2) is not orthogonally adjacent to any box. Therefore, nothing happens.
- The box at (1, 0) falls to (2, 0).
Nothing happens afterwards.
Example 2:
Input: grid = [["-","*"],["#","-"]]
Output: [["-","*"],["#","-"]]
Explanation:
Nothing happens because the bomb is not orthogonally adjacent to any box.
Example 3:
Input: grid = [["-","*","#"],["#","-","-"]]
Output: [["-","*","-"],["-","-","-"]]
Explanation:
The 1^st second:
- The bomb at (0, 1) is orthogonally adjacent to the box at (0, 2). Therefore, the boxes at (0, 2) and (1, 0) become empty cells.
Nothing happens afterwards.
Example 4:
Input: grid = [["#","*","-","#"],["-","-","*","-"]]
Output: [["-","*","-","-"],["-","-","*","-"]]
Explanation:
The 1^st second:
- Since the bomb at (0, 1) is orthogonally adjacent to the box at (0, 0), the box at (0, 0) becomes an empty cell. The other bomb at (1, 2) is not orthogonally adjacent to any box.
- The box at (0, 3) falls to (1, 3).
The 2^nd second:
- Since the bomb at (1, 2) is orthogonally adjacent to the box at (1, 3), the box at (1, 3) becomes an empty cell. The other bomb at (0, 1) is not orthogonally adjacent to any box.
Nothing happens afterwards.
1 <= m == grid.length <= 1001 <= n == grid[i].length <= 100grid[i][j]is'#','-', or'*'.
Solution
Approach: Direct Simulation
Simulate second by second until nothing changes (no box is destroyed and no box moves), which is the final grid.
- Explosions. On the grid at the start of the second, every bomb with a box directly above, below, left or right explodes. Collect the 8 neighbors of all such bombs first, then turn the boxes among them into empty cells, so all explosions are simultaneous.
- Gravity. Scan each column from the bottom up and mark a box as static if it is in the last row, directly above a bomb, or directly above a static box. Every other box moves down one row at the same moment: clear its old cell, then place a box in the cell below it in a new grid.
Boxes only get destroyed or move down, so the process stops.
Complexity: Each second costs O(mn). The number of seconds is at most about m plus the number of boxes destroyed, and in practice far fewer. Space is O(mn).
class Solution:
def finalGrid(self, grid: List[List[str]]) -> List[List[str]]:
m, n = len(grid), len(grid[0])
g = [row[:] for row in grid]
while True:
# explosions, decided on the grid at the start of the second
gone = []
for i in range(m):
for j in range(n):
if g[i][j] != "*":
continue
near = False
for di, dj in ((-1, 0), (1, 0), (0, -1), (0, 1)):
x, y = i + di, j + dj
if 0 <= x < m and 0 <= y < n and g[x][y] == "#":
near = True
if near:
for x in range(max(0, i - 1), min(m, i + 2)):
for y in range(max(0, j - 1), min(n, j + 2)):
gone.append((x, y))
changed = False
for x, y in gone:
if g[x][y] == "#":
g[x][y] = "-"
changed = True
# gravity: find the static boxes from the bottom up
static = [[False] * n for _ in range(m)]
for j in range(n):
for i in range(m - 1, -1, -1):
if g[i][j] == "#":
static[i][j] = (
i == m - 1
or g[i + 1][j] == "*"
or (g[i + 1][j] == "#" and static[i + 1][j])
)
ng = [row[:] for row in g]
for i in range(m):
for j in range(n):
if g[i][j] == "#" and not static[i][j]:
ng[i][j] = "-"
for i in range(m):
for j in range(n):
if g[i][j] == "#" and not static[i][j]:
ng[i + 1][j] = "#"
changed = True
g = ng
if not changed:
return g