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.
classResult{/* * 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 */publicstaticList<String>bomberMan(intn,List<String>grid){// Write your code hereif(n==1)returngrid;introw=grid.size();intcol=grid.get(0).length();if(n%2==0){returncreateZeroGrid(row,col);}// For odd values of n greater than 1, we simulate explosions// There are only two explosion patterns:// - one that occurs at time = 3 (and every 4 seconds after that)// - another that occurs at time = 5 (and every 4 seconds after that)List<String>after3=afterExplosion(grid,row,col);List<String>after5=afterExplosion(after3,row,col);if(n%4==3){returnafter3;}else{returnafter5;}}publicstaticList<String>createZeroGrid(introw,intcol){List<String>result=newArrayList<>();Strings="O".repeat(col);for(inti=0;i<row;i++){result.add(s);}returnresult;}publicstaticList<String>afterExplosion(List<String>grid,introw,intcol){char[][]resultCharArr=newchar[row][col];for(inti=0;i<row;i++){Arrays.fill(resultCharArr[i],'O');}for(inti=0;i<row;i++){for(intj=0;j<col;j++){if(grid.get(i).charAt(j)=='O')//if it's a bomb{//blast that index and i+1,j and i-1,j// and i,j+1 and i,j-1 // total 5 blast from 1 bomb resultCharArr[i][j]='.';if(i>0)resultCharArr[i-1][j]='.';if(i<row-1)resultCharArr[i+1][j]='.';if(j>0)resultCharArr[i][j-1]='.';if(j<col-1)resultCharArr[i][j+1]='.';}}}// Convert the 2D char grid back into a list of stringsList<String>result=newArrayList<>();for(char[]rowOFchar:resultCharArr){result.add(newString(rowOFchar));}returnresult;}}
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 →
java