Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank The Bomberman Game problem solution

YASH PAL, 31 July 202414 December 2025

In this HackerRank The Bomberman Game problem solution, 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 neighbouring cells. This means that if a bomb detonates in cell i,j, any valid cells (i-1,i+1,j) and (i,j+1,j-1) are cleared. If there is a bomb in a neighbouring cell, the neighbouring 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.

HackerRank The Bomberman Game problem solution

Hackerrank The Bomberman Game problem solution in Python programming.

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])))
        
    

The Bomberman Game problem solution in Java Programming.

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);
        
    }
}

Problem solution in C++ programming.

#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;
}

Problem solution in C programming.

#include <stdio.h>

int main()
{
    int n, r, c, i, j;
    char grid0[200][201], grid3[200][200], grid5[200][200];

    scanf("%d %d %dn", &r, &c, &n);
    for (i = 0; i < r; ++i)
        scanf("%sn", grid0[i]);

    for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j)
        if ((grid0[i][j] == 'O') ||
            (i > 0 && grid0[i-1][j] == 'O') ||
            (j > 0 && grid0[i][j-1] == 'O') ||
            (i < r-1 && grid0[i+1][j] == 'O') ||
            (j < c-1 && grid0[i][j+1] == 'O'))
            grid3[i][j] = '.';
        else
            grid3[i][j] = 'O';

    for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j)
        if ((grid3[i][j] == 'O') ||
            (i > 0 && grid3[i-1][j] == 'O') ||
            (j > 0 && grid3[i][j-1] == 'O') ||
            (i < r-1 && grid3[i+1][j] == 'O') ||
            (j < c-1 && grid3[i][j+1] == 'O'))
            grid5[i][j] = '.';
        else
            grid5[i][j] = 'O';

    for (i = 0; i < r; ++i)
    {
        for (j = 0; j < c; ++j)
            printf("%c", n % 2 == 0 ? 'O' : n == 1 ? grid0[i][j] : n % 4 == 3 ? grid3[i][j] : grid5[i][j]);
        printf("n");
    }

    return 0;
}

Problem solution in JavaScript programming.

function processData(input) {
    //Enter your code here
    var lines = input.split("n");
    var line = lines[0];
    var elms = line.split(" ");
    var r = parseInt(elms[0]);
    var c = parseInt(elms[1]);
    var n = parseInt(elms[2]);
    var map = [];
    for(var i = 1; i <= r; i++){
        var tRow = lines[i].split("");
        var row = [];
        for(var j = 0; j < c; j++){
            if(tRow[j] === "."){
                row.push(-1);
            } else {
                row.push(3);
            }
        }
        map.push(row);
    }
    var repeat;
    if(n < 2){
        repeat = 0;
    } else if(n%2 === 0){
        repeat = 2;
    } else if ((n+1)%4 === 0){
        repeat = 3;
    } else if ((n-1)%4 === 0){
        repeat = 5;
    }
    for(var i = 2; i <= repeat; i++){
        for(var j = 0; j < r; j++){
            for(var k = 0; k < c; k++){
                if(i === map[j][k] && i%2 === 1){
                    map[j][k] = -1;
                    if(j !== 0 && map[j-1][k] < i+3 && map[j-1][k] !== i){
                        map[j-1][k] = -1;
                    }
                    if(j !== r-1 && map[j+1][k] < i+3 && map[j+1][k] !== i){
                        map[j+1][k] = -1;
                    }
                    if(k !== 0 && map[j][k-1] < i+3 && map[j][k-1] !== i){
                        map[j][k-1] = -1;
                    }
                    if(k !== c-1 && map[j][k+1] < i+3 && map[j][k+1] !== i){
                        map[j][k+1] = -1;
                    }
                } else if(i%2 === 0 && map[j][k] === -1){
                    map[j][k] = i+3;
                }
            }
        }
    }
    for(var i = 0; i < r; i++){
        var line = "";
        for(var j = 0; j < c; j++){
            if(map[i][j] > -1){
                line+="O";
            } else {
                line += ".";
            }
        }
        console.log(line);
    }
} 

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

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

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes