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.
Python. Runs the clock and does explosions until a cycle is found. For explosion steps, grid for each t % 2 == 1 is kept in the memory. Once the cycle is found, number of steps to make through the graph is calculated, as well as the cycle length and with that final_t can be calculated.
defremoveBomb(row,idx):returnrow[:idx]+'.'+row[idx+1:]defgridHash(grid):return"".join(grid)defcreateAllBombsGrid(r,c):return["O"*cfor_inrange(r)]defgridAfterExplode(grid,r,c):new_grid=createAllBombsGrid(r,c)foriinrange(r):forjinrange(c):ifgrid[i][j]=='.':continue# it's a bomb!forxinrange(-1,2):# iter -1..1i_shifted=i+xifi_shifted<0ori_shifted==r:continuenew_grid[i_shifted]=removeBomb(new_grid[i_shifted],j)foryinrange(-1,2):j_shifted=j+yifj_shifted<0orj_shifted==c:continuenew_grid[i]=removeBomb(new_grid[i],j_shifted)returnnew_griddefcalculate_final_t(cycle_start,cycle_end,n):n_minus_cycle_start=n-cycle_startmoves_to_make=n_minus_cycle_start// 2diff=cycle_end-cycle_startcycle_len=diff// 2 + 1remainder=moves_to_make%cycle_lenfinal_t=cycle_start+remainder*2returnfinal_tdefbomberMan(n,grid):r=len(grid)c=len(grid[0])grid_hash=gridHash(grid)grid_to_t={grid_hash:1}t_to_grid={1:grid}ifn==1:returngridifn%2==0:returncreateAllBombsGrid(r,c)fortinrange(3,n+1,2):grid=gridAfterExplode(grid,r,c)ift==n:returngridgrid_hash=gridHash(grid)ifgrid_hashingrid_to_t:breakelse:grid_to_t[grid_hash]=tt_to_grid[t]=gridcycle_start=grid_to_t[grid_hash]cycle_end=t-2final_t=calculate_final_t(cycle_start,cycle_end,n)returnt_to_grid[final_t]
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 →
Python. Runs the clock and does explosions until a cycle is found. For explosion steps, grid for each t % 2 == 1 is kept in the memory. Once the cycle is found, number of steps to make through the graph is calculated, as well as the cycle length and with that final_t can be calculated.