We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#!/bin/python3importmathimportosimportrandomimportreimportsys## 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=[]forsingrid:res.append(''.join(['.'ifx<=0else'O'forxins]))returnresdef_detonate(grid,i,j):grid[i][j]=0ifi>0andgrid[i-1][j]!=1:grid[i-1][j]=0ifj>0andgrid[i][j-1]!=1:grid[i][j-1]=0ifi<len(grid)-1andgrid[i+1][j]!=1:grid[i+1][j]=0ifj<len(grid[0])-1andgrid[i][j+1]!=1:grid[i][j+1]=0def_plant(grid):foriinrange(len(grid)):forjinrange(len(grid[i])):if(grid[i][j]<=0):grid[i][j]=3passdef_next_tick(grid,tick):iftick==2:returnforiinrange(len(grid)):forjinrange(len(grid[i])):ifgrid[i][j]>1:grid[i][j]-=1iftick==1ortick%2==0andtick!=2:_plant(grid)returnforiinrange(len(grid)):forjinrange(len(grid[i])):ifgrid[i][j]==1:_detonate(grid,i,j)def_to_grid(grid):res=[]forsingrid:res.append([0ifx=='.'else3forxins])returnresdef_to_grid(grid):res=[]forsingrid:res.append([0ifx=='.'else3forxins])returnresdefbomberMan(n,grid):ifn%2==0:return['O'*len(grid[0])]*len(grid)grid=_to_grid(grid)ifn==1:return_to_strings(grid)foriinrange(1,min(8,4+n%4+1)):_next_tick(grid,i)return_to_strings(grid)# Write your code hereif__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_inrange(r):grid_item=input()grid.append(grid_item)result=bomberMan(n,grid)fptr.write('\n'.join(result))fptr.write('\n')fptr.close()fptr.close()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Bomberman Game
You are viewing a single comment's thread. Return to all comments →
My solution: