Sort by

recency

|

368 Discussions

|

  • + 0 comments

    Javascript

    function bomberMan(n, grid) {
        let row = grid.length, column = grid[0].length
        let arr1 = Array.from(Array(grid.length), () => new Array(grid[0].length).fill('O'))
        if (n == 1)
            return grid
        if (n % 2 == 0) {
            for (let i = 0; i < row; i++)
                arr1[i] = arr1[i].join("")
            return arr1
        }
        function boom(cur) {
            let arr = Array.from(Array(row), () => new Array(column).fill('O'));
            for (let r = 0; r < row; r++) {
                for (let c = 0; c < column; c++) {
                    if (cur[r][c] == 'O') {
                        arr[r][c] = "."
                        if (r + 1 < row)
                            arr[r + 1][c] = '.'
                        if (r - 1 >= 0)
                            arr[r - 1][c] = '.'
                        if (c + 1 < column)
                            arr[r][c + 1] = '.'
                        if (c - 1 > -1)
                            arr[r][c - 1] = '.'
                    }
                }
            }
            for (let i = 0; i < row; i++)
                arr[i] = arr[i].join("")
            return arr
        }
        let first = boom(grid)
        let second = boom(first)
        return (n % 4 == 3) ? first : second
    
    }
    
  • + 0 comments

    python

    def bomberMan(n, grid):
        # Write your code here
        if n == 1:
            return grid
        
        if n % 2 == 0:
            return ['O'*c for i in range(r)]
    
        def detonate_bombs(grid):
            new_grid = [['O']*c for i in range(r)]
            
            for i in range(r):
                for j in range(c):
                    if grid[i][j] == 'O':
                        new_grid[i][j] = '.'
                        for x, y in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]:
                            if 0<=x<r and 0<=y<c:
                                new_grid[x][y] = '.'
                                
            return [''.join(row) for row in new_grid]
        
        n //= 2
        for _ in range((n + 1) % 2 + 1):
            new_grid = detonate_bombs(grid)
            grid = new_grid
            
        return [''.join(row) for row in grid]
    
  • + 0 comments

    N(nothing) only happens at 2 seconds. After that bombs are detonating or you are planting bombs.

    P0->N->P1->D0->P2->D1->P3->D2->P4->D3->....etc

    The question tells you this indirectly as the "nothing" is step 2, and we are told to repeat step 3 and 4. Insulation types for hot climates

  • + 0 comments

    Instructions: Here are some helpful tips:

    Convert the list into a 2D matrix of integers. Assign state codes to each cell. Use pen and paper: Start by defining an initial state for the matrix (e.g., a 3x3 grid for simplicity). Write down the matrix for each second, step by step, until you observe a repeating pattern or a cycle.

    Optimization Tips:

    Handle edge cases efficiently:

    Quickly return results for trivial cases or repeating sequences.

    Focus on patterns:

    Once you identify a repeating pattern, determine if you need to calculate the states for all seconds individually.

    Simplify your approach:

    Assess whether storing the state codes as integers is necessary for solving the problem.

  • + 0 comments

    My solution:

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'bomberMan' function below.
    #
    # The function is expected to return a STRING_ARRAY.
    # The function accepts following parameters:
    #  1. INTEGER n
    #  2. STRING_ARRAY grid
    #
        
          
    def _to_strings(grid):
        res=[]
        for s in grid:
            res.append(''.join(['.' if x<=0 else 'O' for x in s]))
        return res
        
        
    def _detonate(grid, i, j):
        grid[i][j]=0
        if i>0 and grid[i-1][j]!=1:
            grid[i-1][j]=0
        if j>0 and grid[i][j-1]!=1:
            grid[i][j-1]=0
        if i<len(grid)-1 and grid[i+1][j]!=1:
            grid[i+1][j]=0
        if j<len(grid[0])-1 and grid[i][j+1]!=1:
            grid[i][j+1]=0
        
        
    def _plant(grid):
        for i in range(len(grid)):
            for j in range(len(grid[i])):
                if (grid[i][j]<=0):
                    grid[i][j]=3
        pass
        
            
    def _next_tick(grid, tick):
        if tick==2:
            return
        for i in range(len(grid)):
            for j in range(len(grid[i])):
                if grid[i][j]>1:
                    grid[i][j]-=1
        if tick==1 or tick%2 == 0 and tick!=2 :
            _plant(grid)
            return
      
        for i in range(len(grid)):
            for j in range(len(grid[i])):
                if grid[i][j]==1:
                    _detonate(grid, i, j)
                    
                   
    def _to_grid(grid):
        res=[]
        for s in grid:
            res.append([0 if x=='.' else 3 for x in s])
        return res
    
    def _to_grid(grid):
        res=[]
        for s in grid:
            res.append([0 if x=='.' else 3 for x in s])
        return res 
    
        
    def bomberMan(n, grid):
        if n%2==0:
            return ['O'*len(grid[0])]*len(grid)
        grid=_to_grid(grid)
        if n==1:
            return _to_strings(grid)
        for i in range(1, min(8, 4+n%4+1)):
            _next_tick(grid, i)
        return _to_strings(grid)
    
    
        
        # Write your code here
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        first_multiple_input = input().rstrip().split()
    
        r = int(first_multiple_input[0])
    
        c = int(first_multiple_input[1])
    
        n = int(first_multiple_input[2])
    
        grid = []
    
        for _ in range(r):
            grid_item = input()
            grid.append(grid_item)
    
        result = bomberMan(n, grid)
    
        fptr.write('\n'.join(result))
        fptr.write('\n')
    
        fptr.close()
    
    
    
        fptr.close()