cswork / OA打开练习工作台

InstacartOA #3

Bubble Popping Game

估计中等评测数据准备中,可先阅读题面

考点(AI 标注)模拟/实现网格搜索

You are given a 2D array grid, where grid[i][j] represents the color of the bubble at position (i, j). You are also given an operations array, which is a sequence of coordinates indicating where to pop bubbles. Rules: If you pop a bubble at a position with value 0 (meaning no bubble), nothing happens. If you pop a bubble, check its top-left, bottom-left, top-right, and bottom-right diagonal neighbors: If the neighbor is within bounds, has a bubble (non-zero), and the color is the same as the popped bubble, that neighbor also pops. After popping, gravity applies: bubbles above empty cells fall down to fill the empty spaces directly below them. Perform all operations in order and return the final grid.

Example 1

Input:

grid = [[1, 2, 3], [6, 1, 2], [1, 2, 5]]
operations = [[1, 1], [1, 2]]

Output:

[[0, 0, 0], [0, 0, 3], [6, 2, 5]]

Explanation: Step 1: Pop bubble at (1, 1)

  • Pops the bubble itself
  • Pops diagonally connected bubble (0, 0) because it has the same color 1 Apply gravity: [ [0, 0, 3], [0, 2, 2], [6, 2, 5] ] Step 2: Pop bubble at (1, 2)
  • Pops the bubble itself
  • Pops diagonal (0, 1) because it's 2
  • Pops diagonal (2, 1) because it's also 2 Apply gravity: [ [0, 0, 0], [0, 0, 3], [6, 2, 5] ] Final Output: [ [0, 0, 0], [0, 0, 3], [6, 2, 5] ]