# The Bomberman Game – HackerRank Solution

In this post, we will solve The Bomberman Game HackerRank Solution. This problem (The Bomberman Game) is a part of HackerRank Problem Solving series.

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Â i, j, any valid cellsÂ (i + 1, j)Â andÂ (i, j + 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.

For example, if the initial grid looks like:

``````...
.O.
...``````

it looks the same after the first second. After the second second, Bomberman has placed all his charges:

``````OOO
OOO
OOO``````

At the third second, the bomb in the middle blows up, emptying all surrounding cells:

``````O.O
...
O.O``````

Function Description

Complete the bomberMan function in the editory below.

bomberMan has the following parameter(s):

• int n: the number of seconds to simulate
• string grid[r]: an array of strings that represents the grid

Returns

• string[r]:Â n array of strings that represent the grid in its final state

## Input Format

The first line contains three space-separated integersÂ r,Â c, andÂ n, The number of rows, columns and seconds to simulate.
Each of the nextÂ rÂ lines contains a row of the matrix as a single string ofÂ cÂ characters. TheÂ `.`Â character denotes an empty cell, and theÂ `O`Â character (ascii 79) denotes a bomb.

## Constraints

• 1 <= r, c <= 200
• 1 <= n <= 109

• 1 <= n <= 200Â forÂ 40%Â of the maximum score.

Sample Input

``````STDIN           Function
-----           --------
6 7 3           r = 6, c = 7, n = 3
.......         grid =['.......', '...O...', '....O..',\
...O...                '.......', 'OO.....', 'OO.....']
....O..
.......
OO.....
OO.....``````

Sample Output

``````OOO.OOO
OO...OO
OOO...O
..OO.OO
...OOOO
...OOOO``````

Explanation

The initial state of the grid is:

``````.......
...O...
....O..
.......
OO.....
OO.....``````

Bomberman spends the first second doing nothing, so this is the state after 1 second:

``````.......
...O...
....O..
.......
OO.....
OO.....``````

Bomberman plants bombs in all the empty cells during his second second, so this is the state after 2 seconds:

``````OOOOOOO
OOOOOOO
OOOOOOO
OOOOOOO
OOOOOOO
OOOOOOO``````

In his third second, Bomberman sits back and watches all the bombs he planted 3 seconds ago detonate. This is the final state afterÂ 3Â seconds:

``````OOO.OOO
OO...OO
OOO...O
..OO.OO
...OOOO
...OOOO``````

## Solution – The Bomberman Game – HackerRank Solution

### C++

```#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include<cstring>
using namespace std;

const int MAXN = 200 + 4;

int n, m, t, a[MAXN][MAXN], b[MAXN][MAXN];
string s[MAXN];

bool fit(int x, int y){return 0 <= x && x < n && 0 <= y && y < m;};

void f(int z){
memcpy(b, a, sizeof(a));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
for (int ii = -1; ii <= 1; ii++)
for (int jj = -1; jj <= 1; jj++)
if (abs(ii) + abs(jj) <= 1 && fit(i + ii, j + jj) && a[i + ii][j + jj] == z - 3)
b[i][j] = -1;
memcpy(a, b, sizeof(b));
}

int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> t;
for (int i = 0; i < n; i++){
cin >> s[i];
for (int j = 0; j < m; j++)
if (s[i][j] == 'O')
a[i][j] = 0;
else
a[i][j] = -1;
}
t--;
t %= 24;
for (int i = 2; i <= t+1; i++){
if (i % 2 == 0){
for (int ii = 0; ii < n; ii++)
for (int jj = 0; jj < m; jj++)
if (a[ii][jj] == -1)
a[ii][jj] = i;
}
else{
f(i);
}
}
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++)
if (a[i][j] != -1)
cout << 'O';
else
cout << '.';
cout << "\n";
}
return 0;
}
```

### Python

```def bomb(b,r,c):
field = [['O' for i in range(c)] for j in range(r)]
for i in range(r):
for j in range(c):
if b[i][j] == 'O':
field[i][j] = '.'
if i+1<r:
field[i+1][j] = '.'
if i>0:
field[i-1][j] = '.'
if j+1<c:
field[i][j+1] = '.'
if j>0:
field[i][j-1] = '.'
return field

r,c,n = input().split()
r,c,n = int(r),int(c),int(n)
b = []
for i in range(r):
row = list(input())
b.append(row)
if n%2==0:
f = [['O' for i in range(c)] for j in range(r)]
for i in range(r):
print(''.join(map(str,f[i])))
else:
bombed1 = bomb(b,r,c)
bombed2 = bomb(bombed1,r,c)

if n==1:
for i in range(r):
print(''.join(map(str,b[i])))
elif (n+1)%4==0:
for i in range(r):
print(''.join(map(str,bombed1[i])))
elif (n+2)%4==0:
for i in range(r):
print(''.join(map(str,b[i])))
else:
for i in range(r):
print(''.join(map(str,bombed2[i])))
```

### Java

```import java.io.*;
import java.util.*;

public class Solution {

private static int[][] tickFill(int[][] in) {
int[][] out = new int[in.length][];
for (int i = 0; i < in.length; i++) {
out[i] = new int[in[i].length];
for (int j = 0; j < in[i].length; j++) {
if (in[i][j] == 1) {
throw new RuntimeException("Bomb on a fill");
} else {
out[i][j] = in[i][j] == 0 ? 3 : (in[i][j] - 1);
}
}
}
return out;
}

private static int[][] tick(int[][] in) {
int[][] out = new int[in.length][];
for (int i = 0; i < in.length; i++) {
out[i] = in[i].clone();
for (int j = 0; j < in[i].length; j++) {
if (out[i][j] > 0) {
out[i][j]--;
}
}
}
for (int i = 0; i < in.length; i++) {
for (int j = 0; j < in[i].length; j++) {
if (in[i][j]==1) {
if (i > 0) {
out[i-1][j] = 0;
}
if (i < in.length-1) {
out[i+1][j] = 0;
}
if (j > 0) {
out[i][j-1] = 0;
}
if (j < in[i].length-1) {
out[i][j+1] = 0;
}
}
}
}
return out;
}

public static void p(int[][] bs) {
for (int i = 0; i < bs.length; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < bs[i].length; j++) {
sb.append(bs[i][j]>0?'O':'.');
}
System.out.println(sb);
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
int c = sc.nextInt();
int n = sc.nextInt();
sc.nextLine();

int[][] bs = new int[r][];
for (int i = 0; i < r; i++) {
String s = sc.nextLine();
bs[i] = new int[c];
for (int j = 0; j < c; j++) {
bs[i][j] = s.charAt(j)=='O'?3:0;
}
}

if (n >= 200) {
n = ((n-1) % 4)+5;
}

for (int i = 1; i <= n; i++) {
if (i % 2 == 1) {
bs = tick(bs);
} else {
bs = tickFill(bs);
}
//System.out.println("- "+i);
//p(bs);
}
p(bs);

}
}
```

Note: This problem (The Bomberman Game) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.