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.
Observation:
There is a pattern:
--> The blast at 3rd second, 7th second, 11th second.... will be the same.
--> Similarly, the blast at 5th second, 9th second, ... and so on will be the same.
What we can do is to create a function that gets us the output grid for the 3rd second (1st blast). Now if we run that function again with the input of the 1st blast, we will get the output for the 5th second (2nd blast)
My code:
constfilledGrid=(grid)=>{returngrid.map((row)=>row.split("").map((ele)=>"O").join(""));};constsecondPattern=(grid)=>{letnewGrid=grid.map((row)=>row.split("").map((el)=>"O"));for(leti=0;i<grid.length;i++){for(letj=0;j<grid[0].length;j++){if(grid[i][j]==="O"){newGrid[i][j]=".";if(i>0){newGrid[i-1][j]=".";}if(j>0){newGrid[i][j-1]=".";}if(i<grid.length-1){newGrid[i+1][j]=".";}if(j<grid[0].length-1){newGrid[i][j+1]=".";}}}}returnnewGrid.map((row)=>row.join(""));};functionbomberMan(n,grid){// Write your code hereif(n===1)returngrid;if(n%2===0)returnfilledGrid(grid);letsecondPatternMatch=false;for(leti=2;i<=n;i++){if(i%2!==0){secondPatternMatch=!secondPatternMatch;}}returnsecondPatternMatch?secondPattern(grid):secondPattern(secondPattern(grid));}
What I am doing is using a flag variable to check the seconds.
`
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 →
Observation: There is a pattern: --> The blast at 3rd second, 7th second, 11th second.... will be the same. --> Similarly, the blast at 5th second, 9th second, ... and so on will be the same. What we can do is to create a function that gets us the output grid for the 3rd second (1st blast). Now if we run that function again with the input of the 1st blast, we will get the output for the 5th second (2nd blast)
My code:
What I am doing is using a flag variable to check the seconds.
`