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.
This took a long time for me to solve, and the reason was I just didn't consider the property of a pattern that it always repeat. The solution is there could be only 4 right answers
1. Bombs in all rooms
2. Bombs in room as an input
3. Bombs in room after 1st blast
4. Bombs in room after 2nd blast
Here is my solution in golang
funcbomberMan(nint32,grid[]string)[]string{// Write your code hereifn==1{returngrid}ifn%2==0{returngenerateAllBomb(len(grid),len(grid[0]))}detonate(grid)if(n+1)%4==0{returngrid}returndetonate(grid)}funcgenerateAllBomb(rows,colsint)[]string{allBombs:=make([]string,rows)fori:=0;i<rows;i++{allBombs[i]=strings.Repeat("O",cols)}returnallBombs}funcdetonate(grid[]string)[]string{typebombCoordinatestruct{x,yint}bombCoordinates:=make([]bombCoordinate,0)fori,s:=rangegrid{forj,r:=ranges{ifr=='O'{bombCoordinates=append(bombCoordinates,bombCoordinate{i,j})}grid[i]=strings.ReplaceAll(grid[i],".","O")}}gl,il:=len(grid),len(grid[0])for_,bc:=rangebombCoordinates{grid[bc.x]=grid[bc.x][:bc.y]+"."+grid[bc.x][bc.y+1:]ifbc.y-1>=0{grid[bc.x]=grid[bc.x][:bc.y-1]+"."+grid[bc.x][bc.y:]}ifbc.x-1>=0{grid[bc.x-1]=grid[bc.x-1][:bc.y]+"."+grid[bc.x-1][bc.y+1:]}ifbc.y+1<il{grid[bc.x]=grid[bc.x][:bc.y+1]+"."+grid[bc.x][bc.y+2:]}ifbc.x+1<gl{grid[bc.x+1]=grid[bc.x+1][:bc.y]+"."+grid[bc.x+1][bc.y+1:]}}returngrid}
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 →
This took a long time for me to solve, and the reason was I just didn't consider the property of a pattern that it always repeat. The solution is there could be only 4 right answers 1. Bombs in all rooms 2. Bombs in room as an input 3. Bombs in room after 1st blast 4. Bombs in room after 2nd blast
Here is my solution in golang