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.
publicstaticList<String>bomberMan(intn,List<String>grid){// Write your code hereList<String>holeGrid=newArrayList<String>();introw=grid.size();intcol=grid.get(0).length();char[][]newGrid=newchar[row][col];for(inti=0;i<row;i++){for(intj=0;j<col;j++){newGrid[i][j]='O';}}for(char[]line:newGrid){StringnewLine=newString(line);holeGrid.add(newLine);}List<String>grid1=nextState(grid);List<String>grid2=nextState(grid1);List<String>grid3=nextState(grid2);if(n==1){returngrid;}if(n==3){returngrid1;}if(n%2==0){returnholeGrid;}intflipTime=(n-1)/2;if(flipTime%2==1){returngrid3;}returngrid2;}publicstaticList<String>nextState(List<String>grid){List<String>nextGrid=newArrayList<String>();introw=grid.size();intcol=grid.get(0).length();char[][]newGrid=newchar[row][col];int[][]directions={{0,0},{-1,0},{0,-1},{1,0},{0,1}};for(inti=0;i<row;i++){for(intj=0;j<col;j++){newGrid[i][j]='O';}}for(inti=0;i<grid.size();i++){for(intj=0;j<grid.get(0).length();j++){if(grid.get(i).charAt(j)=='O'){for(int[]direction:directions){intj_new=j+direction[0];inti_new=i+direction[1];if(j_new<col&&j_new>=0&&i_new<row&&i_new>=0){newGrid[i_new][j_new]='.';}}}}}for(char[]line:newGrid){StringnewLine=newString(line);nextGrid.add(newLine);}returnnextGrid;}
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 →
Flowing is my java 8 solution