Sort by

recency

|

367 Discussions

|

  • + 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()
    
  • + 0 comments

    def bomberMan(n, grid): if n% 2 == 0: return ["O"*len(grid[0])]*len(grid)

    k = 1 if (n-1)% 4 != 0 else (0 if n==1 else 2)
    for _ in range(k):
        d1 = []
        d2 = []
        grid_2 = [''.join(x) for x in zip(*grid)]
        for nap, dn in ((grid, d1), (grid_2, d2)):
            for i in nap:
                gr = i.replace(".", '0').replace("O", '1')
                gr2 = int(gr, 2) | int(gr, 2) <<1
                gr3 = int(gr +'0', 2) | gr2 >>1
                dn.append(bin(gr3)[2:].zfill(len(i) + 1)[1:])
        d3 = [''.join(x) for x in zip(*d2)]
        d4 = [(int(p, 2) | int(v, 2)) for (p, v) in zip(d1, d3)]
        d4s = [bin(x + 2 ** len(grid[0]))[3:] for x in d4]
        grid = [i.replace("0", "O").replace("1", ".") for i in d4s]
    
    return grid