[Python] I have tried this question and couldn't able to get exact expected output? [closed]

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    [enter image description here][1]The below provided is the question that I am not getting and it would be very helpful if anyone can help with it.

    it is a gravity simulation problem with falling boxes.

    Find the question below:

    You are running a gravity simulation involving falling boxes and exploding obstacles. The scenario is represented by a rectangular matrix of characters board.

    Each cell of the matrix board contains one of three characters:

    - the cell is empty
    * the cell contains an obstacle
    # the cell contains a box


    The boxes all fall down simultaneously as far as possible, until they hit an obstacle, another grounded box, or the bottom of the board. If a box hits an obstacle, the box explodes, destroying itself and any other boxes within eight cells surrounding the obstacle. All the explosions happen simultaneously as well.

    Given board, your task is to return the state of the board after all boxes have fallen.

    Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(board[0].length · board.length2) will fit within the execution time limit.

    Example 1:

    board = [['#', '-', '#', '#', '*'],
    ['#', '-', '-', '#', '#'],
    ['-', '#', '-', '#', '-'],
    ['-', '-', '#', '-', '#'],
    ['#', '*', '-', '-', '-'],
    ['-', '-', '*', '#', '-']]


    The output should be:

    solution = [['-', '-', '-', '-', '*'],
    ['-', '-', '-', '-', '-'],
    ['-', '-', '-', '-', '-'],
    ['-', '-', '-', '-', '-'],
    ['-', '*', '-', '-', '#'],
    ['#', '-', '*', '-', '#']]


    Example 2:

    board = [['#', '#', '*'],
    ['#', '-', '*'],
    ['#', '-', '*'],
    ['-', '#', '#'],
    ['*', '-', '#'],
    ['*', '-', '-'],
    ['*', '-', '-']]


    The output should be:

    solution = [['-', '-', '*'],
    ['-', '-', '*'],
    ['-', '-', '*'],
    ['-', '-', '-'],
    ['*', '-', '-'],
    ['*', '-', '#'],
    ['*', '-', '#']]


    Example video:

    [​IMG]

    Contraints:


    • Execution time limit: 4 seconds (py3)


    • Memory limit: 1 GB


    • Input: array.array.char board
      A matrix that shows where the boxes and obstacles are located. The matrix consists only of characters '-', '*', and '#'.


    • 3 ≤ board.length ≤ 100,


    • 3 ≤ board.length ≤ 100.


      [*]
      Output: array.array.char
      Return a matrix representing the state of the board after all of the boxes have fallen.



    As you can see in the image that boxes are falling and getting exploded.

    My code that is not getting that exact output is given below:

    def solution(board):
    rows, cols = len(board), len(board[0])

    # Helper function to handle explosions
    def explode(r, c):
    # All 8 directions + the current cell
    directions = [(-1, -1), (-1, 0), (-1, 1),
    (0, -1), (0, 1),
    (1, -1), (1, 0), (1, 1)]
    for dr, dc in directions:
    nr, nc = r + dr, c + dc
    if 0 <= nr < rows and 0 <= nc < cols and board[nr][nc] == '#':
    board[nr][nc] = '-' # Destroy nearby box

    # Simulate the falling process
    for col in range(cols):
    empty_row = rows - 1 # Start from the bottom of the column
    for row in range(rows - 1, -1, -1):
    if board[row][col] == '#': # Box found
    if empty_row != row:
    board[empty_row][col] = '#' # Move box down
    board[row][col] = '-' # Clear original box location
    empty_row -= 1 # Move the empty row pointer up
    elif board[row][col] == '*': # Obstacle found
    # Trigger explosion for any box above
    for r in range(row - 1, -1, -1):
    if board[r][col] == '#':
    explode(r, col)
    board[r][col] = '-'
    empty_row = row - 1

    return board


    The output I am getting is given below:

    [['-', '-', '-', '-', '*'],
    ['-', '-', '-', '-', '-'],
    ['-', '-', '-', '-', '-'],
    ['-', '-', '-', '-', '-'],
    ['#', '*', '-', '#', '#'],
    ['#', '-', '*', '#', '#']]

    which is different from expected output

    Continue reading...

Compartilhe esta Página