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.
vector<string>bomberMan(intn,vector<string>grid){autonew_grid=grid;introws=grid.size();intcols=grid[0].size();// the same answers are cycling over, just need to pick the correct oneif(n>4){n=n%4+4;}// bomb identifier - switching between 'O' and 'x' to differentiate time of plantingcharb='x';for(inti=2;i<=n;i++){if(i%2==0){// step 3 - filling empty fields with bombsfor(intr=0;r<rows;r++){for(intc=0;c<cols;c++){if(grid[r][c]=='.'){grid[r][c]=b;}}}// flipping bomb typeif(b=='x')b='O';elseb='x';}else{// step 4 - exploding bombs planted at time i-3new_grid=grid;for(intr=0;r<rows;r++){for(intc=0;c<cols;c++){if(grid[r][c]==b){if(r>0)new_grid[r-1][c]='.';if(r<rows-1)new_grid[r+1][c]='.';if(c>0)new_grid[r][c-1]='.';if(c<cols-1)new_grid[r][c+1]='.';new_grid[r][c]='.';}}}swap(new_grid,grid);}}// going back to default bomb labelingfor(intr=0;r<rows;r++){for(intc=0;c<cols;c++){if(grid[r][c]=='x')grid[r][c]='O';}}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 →
C++ solution with comments: