Header Ad

HackerRank The Bomberman Game problem solution

In this HackerRank The Bomberman Game problem, you have 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


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


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 %d\n", &r, &c, &n);
    for (i = 0; i < r; ++i)
        scanf("%s\n", 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);
});


Post a Comment

5 Comments

  1. what does below code perform:
    if (n >= 200) {
    n = ((n-1) % 4)+5;
    }

    ReplyDelete
    Replies
    1. if the value of n is greater than 200 then the new value of n become the n-1 and then module by 4 (that's remaining) and then add 5 to it. now it assign the new value to n.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. This comment has been removed by the author.

      Delete
    4. Hi @YASH PAL. How can you know that rule? Thanks

      Delete