Problem · Matrix

Falling Boxes and Exploding Obstacles

Learn this problem
MediumxAI logoxAIFULLTIMEOA

Problem statement

A rectangular character matrix board contains:

  • - for an empty cell,
  • * for a fixed obstacle, and
  • # for a box.

Gravity advances in simultaneous one-row rounds. Boxes that are already supported by the bottom or by a grounded stack do not move. Every other box falls one row. If its next cell is an obstacle, the box collides with that obstacle and disappears instead of entering the cell; boxes higher in the same falling stack still advance one row during that round.

After all movement and collisions in a round, every obstacle hit in that round explodes simultaneously. An explosion destroys every box in the obstacle's surrounding 3 x 3 neighborhood, while obstacles remain. Continue with another gravity round until no box moves or collides.

Return the final board.

Function

simulateFallingBoxes(board: char[][]) → char[][]

Examples

Example 1

board = [["#","-","#","#","*"],["#","-","-","#","#"],["-","#","-","#","-"],["-","-","#","-","#"],["#","*","-","-","-"],["-","-","*","#","-"]]return = [["-","-","-","-","*"],["-","-","-","-","-"],["-","-","-","-","-"],["-","-","-","-","-"],["-","*","-","-","#"],["#","-","*","-","#"]]

Boxes advance through repeated simultaneous rounds. The obstacles at row 4, column 1 and row 5, column 2 are hit; their round-specific explosions remove nearby falling boxes before the surviving boxes settle.

Example 2

board = [["#","#","*"],["#","-","*"],["#","-","*"],["-","#","#"],["*","-","#"],["*","-","-"],["*","-","-"]]return = [["-","-","*"],["-","-","*"],["-","-","*"],["-","-","-"],["*","-","-"],["*","-","#"],["*","-","#"]]

Boxes in the first column repeatedly collide with its obstacle, while the two surviving boxes in the last column settle at the bottom.

Constraints

  • 3 <= board.length <= 100.
  • 3 <= board[i].length <= 100.
  • Every row has the same length.
  • Every cell is -, *, or #.

More xAI problems

drafts saved locally
public char[][] simulateFallingBoxes(char[][] board) {
    // Write your code here.
}
board[["#","-","#","#","*"],["#","-","-","#","#"],["-","#","-","#","-"],["-","-","#","-","#"],["#","*","-","-","-"],["-","-","*","#","-"]]
expected[["-","-","-","-","*"],["-","-","-","-","-"],["-","-","-","-","-"],["-","-","-","-","-"],["-","*","-","-","#"],["#","-","*","-","#"]]
checking account