# HackerRank The Bomberman Game Solution

Hello Programmers, In this post, you will learn how to solve HackerRank The Bomberman Game Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

## HackerRank The Bomberman Game

Bomberman lives in a rectangular grid. Each cell in the grid either contains a bomb or nothing at all.

Each bomb can be planted in any cell of the grid but once planted, it will detonate after exactly 3 seconds. Once a bomb detonates, it’s destroyed — along with anything in its four neighboring cells. This means that if a bomb detonates in cell ij, any valid cells (i + 1, j) and (ij + 1) are cleared. If there is a bomb in a neighboring cell, the neighboring bomb is destroyed without detonating, so there’s no chain reaction.

Bomberman is immune to bombs, so he can move freely throughout the grid. Here’s what he does:

1. Initially, Bomberman arbitrarily plants bombs in some of the cells, the initial state.
2. After one second, Bomberman does nothing.
3. After one more second, Bomberman plants bombs in all cells without bombs, thus filling the whole grid with bombs. No bombs detonate at this point.
4. After one more second, any bombs planted exactly three seconds ago will detonate. Here, Bomberman stands back and observes.
5. Bomberman then repeats steps 3 and 4 indefinitely.

Note that during every second Bomberman plants bombs, the bombs are planted simultaneously (i.e., at the exact same moment), and any bombs planted at the same time will detonate at the same time.

Given the initial configuration of the grid with the locations of Bomberman’s first batch of planted bombs, determine the state of the grid after N seconds.

Function Description

Complete the absolutePermutation function in the editor below.

absolutePermutation has the following parameter(s):

• int n: the upper bound of natural numbers to consider, inclusive
• int k: the absolute difference between each element’s value and its index

Returns

• int[n]: the lexicographically smallest permutation, or [-1] if there is none

Input Format

The first line contains an integer t, the number of queries.
Each of the next t lines contains 2 spaceseparated integers, n and k.

Constraints

• 1 <= t <= 10
• 1 <= n <= 105
• 0 <= k < n

Sample Input

```STDIN   Function
-----   --------
3       t = 3 (number of queries)
2 1     n = 2, k = 1
3 0     n = 3, k = 0
3 2     n = 3, k = 2
```

Sample Output

2 1
1 2 3
-1

Explanation

Test Case 0:

Test Case 1:

Test Case 2:
No absolute permutation exists, so we print `-1` on a new line.

## HackerRank The Bomberman Game Solution

### The Bomberman Game Solution in C

```#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int r,c,n;
char set1[201][201];
char set2[201][201];
char set3[201][201];
char set4[201][201];
scanf("%d %d %d",&r,&c,&n);
for(int i=0;i<r;i++)
{
scanf("%s",set1[i]);
for(int j=0;j<c;j++)
{
set2[i][j]=79;
set3[i][j]=79;
set4[i][j]=79;
}
}

for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(set1[i][j]==79)
{
set3[i][j]='.';
set3[i][j-1]='.';
set3[i][j+1]='.';
set3[i-1][j]='.';
set3[i+1][j]='.';
}
}
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(set3[i][j]==79)
{
set4[i][j]='.';
set4[i][j-1]='.';
set4[i][j+1]='.';
set4[i-1][j]='.';
set4[i+1][j]='.';
}
}
}
if(n%2==0)
{
//print set 2
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
printf("%c",set2[i][j]);
}
printf("\n");
}
}
else if(n==1)
{
//print set 1
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
printf("%c",set1[i][j]);
}
printf("\n");
}
}
else if(n%4==1)
{
//print set 1
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
printf("%c",set4[i][j]);
}
printf("\n");
}
}
else
{
//print set 3
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
printf("%c",set3[i][j]);
}
printf("\n");
}
}
return 0;
}```

### The Bomberman Game Solution in Cpp

```#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
char ch[300];
int n,m,T,pd[300][300],bo[300][300];
const int gox[4]={1,-1,0,0},goy[4]={0,0,1,-1};
void print(){
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++) if (pd[i][j]==0) putchar('.'); else putchar('O');
cout<<endl;
}
}
int main(){
scanf("%d%d%d",&n,&m,&T);
for (int i=1;i<=n;i++){
scanf("%s",ch+1);
for (int j=1;j<=m;j++) pd[i][j]=(ch[j]=='O');
}
if (T==1){
print(); return 0;
}
if (T%2==0){
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++) pd[i][j]=1;
print(); return 0;
}
memcpy(bo,pd,sizeof bo);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
for (int k=0;k<4;k++)
pd[i][j]|=bo[i+gox[k]][j+goy[k]];
if ((T/2)%2==0){
memcpy(bo,pd,sizeof bo);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++){
pd[i][j]=bo[i][j];
for (int k=0;k<4;k++)
if (i+gox[k]>0&&i+gox[k]<=n&&j+goy[k]>0&&j+goy[k]<=m&&bo[i+gox[k]][j+goy[k]]==0) pd[i][j]=0;
}
print();
}else {
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++) pd[i][j]^=1;
print();
}
}```

### The Bomberman Game Solution in Java

```public class Solution {

public static void toComplement(char[][] grid, int r, int c){
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
if(grid[i][j]=='O'){
if(i-1>=0 && grid[i-1][j]!='O') grid[i-1][j]='*';
if(i+1<r && grid[i+1][j]!='O') grid[i+1][j]='*';
if(j-1>=0 && grid[i][j-1]!='O') grid[i][j-1]='*';
if(j+1<c && grid[i][j+1]!='O') grid[i][j+1]='*';
}
}
}
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
if(grid[i][j]=='.') grid[i][j]='O';
else if(grid[i][j]=='O' || grid[i][j]=='*') grid[i][j]='.';
}
}
}

public static void toFull(char[][] grid, int r, int c){
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
grid[i][j]='O';
}
}
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int r = in.nextInt();
int c = in.nextInt();
int n = in.nextInt();
in.nextLine();
char[][] grid = new char[r][c];
for(int i=0; i<r; i++){
grid[i] = in.nextLine().toCharArray();
}
if(n%2 == 0) toFull(grid, r, c);
if(n%4 == 3) toComplement(grid, r, c);
if(n%4 == 1 && n!=1){
toComplement(grid, r, c);
toComplement(grid, r, c);
}
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
System.out.print(grid[i][j]);
}
System.out.println();
}
}
}```

### The Bomberman Game Solution in Python

```# Enter your code here. Read input from STDIN. Print output to STDOUT
def bomb(r, c, grid, t):
complete_grid = [ ["O"]*c for i in range(r) ]
stable_grid = [ ["O"]*c for i in range(r) ]
other_grid = [ ["O"]*c for i in range(r) ]
for i in range(r):
for j in range(c):
if grid[i][j] == "O":
other_grid[i][j] = "."
if i>0:
other_grid[i-1][j] = "."
if j>0:
other_grid[i][j-1] = "."
if i<r-1:
other_grid[i+1][j] = "."
if j<c-1:
other_grid[i][j+1] = "."
for i in range(r):
for j in range(c):
if other_grid[i][j] == "O":
stable_grid[i][j] = "."
if i>0:
stable_grid[i-1][j] = "."
if j>0:
stable_grid[i][j-1] = "."
if i<r-1:
stable_grid[i+1][j] = "."
if j<c-1:
stable_grid[i][j+1] = "."
#print grid
#print complete_grid
#print other_grid
if t==0 or t==1:
return grid
if (t-1)%2==0 and not (t-1)%4==0:
return other_grid
if (t-1)%4==0:
return stable_grid
if t%2==0:
return complete_grid
grid = []
r, c, t = map(int, raw_input().split())
for _ in range(r):
row = [ l for l in raw_input() ]
grid.append(row)
res = "\n".join([ "".join(line) for line in bomb(r, c, grid, t) ])
print res
```

### The Bomberman Game Solution using JavaScript

```function processData(input) {
var split = input.split("\n");
var [R,C,N] = split[0].split(" ");
var grid = split.slice(1);
for(var i=0; i<grid.length; i++) {
grid[i] = grid[i].split("");
}
}
var states = {}, isRepeating = false;
var rowCount = grid.length;
if(N==1) {
return print(grid);
}
else if(N % 2 == 0) {
return printFalseGrid(grid);
}
else {
let loopFor= (N-1)/2 ; //times
for(var i=0; i<loopFor && !isRepeating; i++) {
grid = transformGrid(grid, (2*i)+3);
}
if(isRepeating) {
for(let prop in states) {
let val = states[prop];
if(val.counter >= 2 && (val.N % 4 == N % 4)) {
return print(val.grid);
}
}
}
else {
print(grid);
}
}
}

function transformGrid(initialState, N) {
var grid = initialState;
var rowCount = grid.length;
var colCount = grid[0].length;
var modifiedGrid = [];
var stateObj = {
false: 0,
true: 0
}
for(var i=0; i<rowCount; i++) {
modifiedGrid.push([]);
for(var j=0; j<colCount; j++) {
let cell = grid[i][j];
modifiedGrid[i].push(cell);
if(!isCellFalse(grid, i, j)) {
stateObj.true++;
}
if(isCellFalse(grid, i, j)) {
modifiedGrid[i][j] = ".";
stateObj.false++;
}
else if(!isCellInDangerZone(grid, i, j)) {
//console.log("i:" + i);
//console.log("j:" + j);
modifiedGrid[i][j] = "O";
}
}
}
var stateString = uniqueStateString(stateObj);
if(typeof states[stateString] === 'undefined') {
states[stateString] = {
counter: 0,
grid: modifiedGrid,
N: N
};
}
else {
states[stateString].counter++;
}
if(states[stateString].counter > 2) {
isRepeating = true;
}
return modifiedGrid;

}
function uniqueStateString(stateObj) {
return stateObj.false+","+stateObj.true
}
function isCellFalse(grid, i, j) {
return grid[i][j] == "O";
}
function isCellInDangerZone(grid,i,j) {
if([grid[i][j-1], grid[i][j+1]].indexOf("O") !== -1) return true;
if(grid[i-1] && grid[i-1][j] == "O") return true;
if(grid[i+1] && grid[i+1][j] == "O") return true;
}
function printFalseGrid(grid) {
for(var k=0; k<grid.length; k++) {
console.log("O".repeat(grid[0].length));
}
}
function print(grid) {
for(var i=0; i<grid.length; i++) {
console.log(grid[i].join(""));
}
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});

process.stdin.on("end", function () {
processData(_input);
});```

### The Bomberman Game Solution in Scala

```import scala.io.StdIn

object Solution {
def main(args: Array[String]) {
val Array(r, c, n) = StdIn.readLine().split("\\s+").map(_.toInt)
var field = (1 to r).map(_ => StdIn.readLine().toArray.map(c => if (c == 'O') '0' else c)).toArray
for (i <- 1 to math.min(n, 200 + n % 4)) {
field = field.map(_.map(c => if (c != '.') {
(c + 1).toChar
} else if (c == '.' && i % 2 == 0) {
'0'
} else {
c
}))

def bomb(x: Int, y: Int) = x >= 0 && x < c && y >= 0 && y < r && field(y)(x) == '3'

if (i % 2 == 1 && i != 1) {
for (y <- 0 until r; x <- 0 until c) {
if (!bomb(x, y) && (bomb(x, y + 1) || bomb(x, y - 1) || bomb(x - 1, y) || bomb(x + 1, y))) {
field(y)(x) = '.'
}
}
for (y <- 0 until r; x <- 0 until c) {
if (bomb(x, y)) {
field(y)(x) = '.'
}
}
}
}
println(field.map(_.map(c => if (c == '.') '.' else 'O').mkString("")).mkString("\n"))
}
}```

### The Bomberman Game Solution in Pascal

```type  grid=array[1..200]of string[200];
var f:text; r,c,i:byte; n:int64;
g:array[0..3]of grid;

function fill:grid;
var g:grid; s:string[200]; i:byte;
begin
s:='';
for i:=1 to c do s:=s+'O';
for i:=1 to r do g[i]:=s;
exit(g)
end;

function invert(g:grid):grid;
var f:grid; i,j:byte;

procedure explode(x,y:byte);
begin
f[x,y]:='.';
if x-1>0 then f[x-1,y]:='.';
if y-1>0 then f[x,y-1]:='.';
if x+1<=r then f[x+1,y]:='.';
if y+1<=c then f[x,y+1]:='.'
end;

begin
f:=fill;
for i:=1 to r do
for j:=1 to c do
if g[i,j]='O' then
explode(i,j);
exit(f)
end;

procedure generate;
begin
g[1]:=g[0]; g[2]:=fill;
g[3]:=invert(g[1]); g[0]:=fill;
g[1]:=invert(g[3])
end;

procedure print(g:grid);
var i:byte;
begin
for i:=1 to r do
writeln(f,g[i])
end;

begin
for i:=1 to r do readln(f,g[0,i]);
close(f);
assign(f,''); rewrite(f);
if n=1 then print(g[0])
else
begin
generate; print(g[n mod 4])
end;
close(f)
end.```

Disclaimer: This problem (The Bomberman Game) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.

## FAQ:

1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume.

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite.

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.