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.
Do some examples on paper and then observe this patter:
The grid is always full with bombs if n is even
The grid at 3 seconds is the detonation of the initial state bombs
The grid at 5 seconds it the detonation of the grid at 3 seconds
Grids 3, 7, 11, ... are repeating
Grids 5, 9, 13, ... are repeating
So the algorithm becomes:
If n is even, return an all bomb grid
Calculate grid at three seconds
Calculate grid at five seconds
If n is 3, 7, 11, ... return three second grid
If n is 5, 9, 13, ... return five second grid
if n < 3, return initial grid
Solution
defget_detonated_grid(bomb_grid):rows=len(bomb_grid)cols=len(bomb_grid[0])detonated_grid=[]foriinrange(rows):detonated_grid.append([])forjinrange(cols):detonated_grid[i].append('O')foriinrange(rows):forjinrange(cols):ifbomb_grid[i][j]=='.':continuedetonated_grid[i][j]='.'ifi>0:detonated_grid[i-1][j]='.'ifi<rows-1:detonated_grid[i+1][j]='.'ifj>0:detonated_grid[i][j-1]='.'ifj<cols-1:detonated_grid[i][j+1]='.'returndetonated_griddefall_bomb_grid(r,c):return[['O']*c]*rdefto_output(grid):return[''.join(row)forrowingrid]defbomberMan(n,grid):ifn==1:returngridall_bombs=all_bomb_grid(r,c)ifn%2==0:returnto_output(all_bombs)# The grid at three seconds is the detonation from initial statethree_second_grid=get_detonated_grid(grid)# The grid at five seconds is the detonation from the bombs# on the three second grid# The 3, 7, 11, ... grids repeat# The 5, 9, 13, ... grids repeatifn==3or(n-3)%4==0:returnto_output(three_second_grid)else:returnto_output(get_detonated_grid(three_second_grid))
The Bomberman Game
You are viewing a single comment's thread. Return to all comments →
Do some examples on paper and then observe this patter:
So the algorithm becomes:
Solution