← 返回 pinterest 的题目列表Bubble Explosion on a Grid (One Round + Gravity)
类型:online_judge
Problem: Bubble Explosion (Single Round) with Gravity
You are given a 2D grid bubbles of integers where bubbles[r][c] is the color of the bubble in that cell. It is guaranteed that initially every cell contains a bubble.
Two cells are neighbors if they share a side (4-directional adjacency: up, down, left, right).
Perform one bubble explosion with the following rules:
A bubble is eligible to explode if its color matches the color of bubbles in at least 2 neighboring cells.
For each color, mark for explosion: all eligible bubbles of that color, plus any bubbles of the same color that are 4-directionally connected to them (same-color connected component expansion).
All marked bubbles explode simultaneously and are removed, producing empty cells.
After removal, bubbles above empty cells fall down within their column to fill gaps (gravity). Newly created top cells become empty.
Return the board after the explosion and gravity, using 0 for empty cells.
Input
bubbles: int[][]
Output
int[][] of the same size, with empty cells as 0.
Constraints (visible in screenshot)
1 ≤ bubbles.length ≤ 100
1 ≤ bubbles[0].length ≤ 100
1 ≤ bubbles[i][j] ≤ 10^4
Example
Input:
bubbles = [
[3, 1, 2, 1],
[1, 1, 1, 4],
[3, 1, 2, 2],
[3, 3, 3, 4]
]
Output:
[
[0, 0, 0, 1],
[0, 0, 0, 4],
[0, 0, 2, 2],
[3, 0, 2, 4]
]
Example
Input
4 4
3 1 2 1
1 1 1 4
3 1 2 2
3 3 3 4
Output
0 0 0 1
0 0 0 4
0 0 2 2
3 0 2 4