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){// 1 sec: return grid.varprePrev=newList<string>(grid);if(n==1)returnprePrev;// even sec: return grid filled with bombs.varprev=grid.Select(s=>newstring('O',s.Length)).ToList();if(n%2==0)returnprev;// detonate once to create result.varresult=DetonateBombs(grid);if(n==3)returnresult;// skip even sec and start detonating from 5,// as 4 secs passed already.varterminatedAt=-1;for(vars=5;s<=n;s+=2){prePrev=newList<string>(prev);prev=newList<string>(result);result=DetonateBombs(result);// if result equals prePrev,// we will have 2 grids repeating till the end.if(result.SequenceEqual(prePrev)){terminatedAt=s;break;};}// check if prev or result should be returned.if(terminatedAt>-1)if((n-terminatedAt)%4!=0)returnprev;returnresult;}publicstaticList<string>DetonateBombs(List<string>grid){varrows=grid.Count;varcols=grid[0].Length;varnewGrid=newList<string>(rows);varinBombRange=(List<string>g,intr,intc)=>{vartop=r-1;varright=c+1;varleft=c-1;vardown=r+1;varrowOutOfRange=(inti)=>i<0||i>g.Count-1;varcolOutOfRange=(inti)=>i<0||i>g[0].Length-1;if(g[r][c]=='O')returntrue;if(!rowOutOfRange(top)&&g[top][c]=='O')returntrue;if(!colOutOfRange(right)&&g[r][right]=='O')returntrue;if(!rowOutOfRange(down)&&g[down][c]=='O')returntrue;if(!colOutOfRange(left)&&g[r][left]=='O')returntrue;returnfalse;};for(varr=0;r<rows;r++){varnewRow="";for(varc=0;c<cols;c++){if(inBombRange(grid,r,c))newRow+=".";elsenewRow+="O";}newGrid.Add(newRow);}returnnewGrid;}
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 →
My C# solution :)