HackerRank The Bomberman Game Solution
In this post, we will solve 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 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:
- Initially, Bomberman arbitrarily plants bombs in some of the cells, the initial state. 2. After one second, Bomberman does nothing.
- 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.
- After one more second, any bombs planted exactly three seconds ago will detonate. Here, Bomberman stands back and observes.
- 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.
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 0 character (ascii 79) denotes a bomb.
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 seconds:
OOO.OOO
OO...OO
OOO...O
..OO.OO
...OOOO
...OOOO
The Bomberman Game C Solution
#include<stdio.h>
#include<stdlib.h>
int main()
{
int q,i,j,a,b,y,z;
unsigned long long int c;
scanf("%d %d %lld",&a,&b,&c);
char x[a][b];
for(i=0;i<a;i++)
{
scanf("%s",x[i]);
}
if(c>200)
c=c%200;
for(q=2;q<=c;q++)
{
if(q%2!=0)
{
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
if(x[i][j]=='o')
{
if(x[i][j+1]!='o'&&j+1<b)
x[i][j+1]='.';
if(x[i][j-1]!='o'&&j-1>=0)
x[i][j-1]='.';
if(x[i+1][j]!='o'&&i+1<a)
x[i+1][j]='.';
if(x[i-1][j]!='o'&&i-1>=0)
x[i-1][j]='.';
x[i][j]='.';
}
}
}
}
if(q%2==0)
{
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
if(x[i][j]=='.')
x[i][j]='O';
else if(x[i][j]=='O')
x[i][j]='o';
}
}
}
}
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
if(x[i][j]=='o')
x[i][j]=x[i][j]-32;
printf("%c",x[i][j]);
}
printf("\n");
}
return 0;
}
The Bomberman Game C++ Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int main() {
int r, c, n; cin >> r >> c >> n;
int g[200][200]; memset(g, 0, sizeof(g));
for (int i = 0; i < r; i++) {
string s; cin >> s;
for (int j = 0; j < c; j++) g[i][j] = (s[j] == 'O' ? 1 : 0);
}
if ((0 != n) && (0 == n%2)) {
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++) g[i][j] = 1;
}
else if (n >= 3) {
int g2[200][200]; memcpy(g2, g, sizeof(g)); // g2 <- g
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (g2[i][j]) {
if (0 != i) g[i-1][j] = 1;
if (i != r-1) g[i+1][j] = 1;
if (0 != j) g[i][j-1] = 1;
if (j != c-1) g[i][j+1] = 1;
}
}
}
memcpy(g2, g, sizeof(g)); // g2 <- g
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (0 == g2[i][j]) {
if (0 != i) g[i-1][j] = 0;
if (i != r-1) g[i+1][j] = 0;
if (0 != j) g[i][j-1] = 0;
if (j != c-1) g[i][j+1] = 0;
}
}
}
//for (int i = 0; i < r; i++) {
// for (int j = 0; j < c; j++) {
// if (0 == g[i][j]) {
// if ((0 != i) && (-1 == g[i-1][j])) g[i-1][j] = 0;
// if ((i != r-1) && (-1 == g[i+1][j])) g[i+1][j] = 0;
// if ((0 != j) && (-1 == g[i][j-1])) g[i][j-1] = 0;
// if ((j != c-1) && (-1 == g[i][j+1])) g[i][j+1] = 0;
// }
// }
//}
//for (int i = 0; i < r; i++) {
// for (int j = 0; j < c; j++) {
// if (-1 == g[i][j]) g[i][j] = 1;
// }
//}
if (0 == ((n-3) % 4)) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (g[i][j]) g[i][j] = -1;
else g[i][j] = 1;
}
}
memcpy(g2, g, sizeof(g)); // g2 <- g
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (-1 == g2[i][j]) {
if (0 != i) g[i-1][j] = 0;
if (i != r-1) g[i+1][j] = 0;
if (0 != j) g[i][j-1] = 0;
if (j != c-1) g[i][j+1] = 0;
g[i][j] = 0;
}
}
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) cout << (g[i][j] ? 'O' : '.');
cout << endl;
}
return 0;
}
The Bomberman Game C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
string[] tokens_rcn = Console.ReadLine().Split(' ');
int r = Convert.ToInt32(tokens_rcn[0]);
int c = Convert.ToInt32(tokens_rcn[1]);
long n = Convert.ToInt64(tokens_rcn[2]);
string[] g0 = new string[r];
for (int i = 0; i < r; i++) {
g0[i] = Console.ReadLine();
}
int[][] g1 = new int[r][];
for (int i = 0; i < r; i++) {
g1[i] = new int[c];
for (int j = 0; j < c; j++) {
if (g0[i][j] == 'O') {
g1[i][j] = 2;
}
else {
g1[i][j] = 0;
}
}
}
int[][] g2 = new int[r][];
for (int i = 0; i < r; i++) {
g2[i] = new int[c];
for (int j = 0; j < c; j++) {
if (g1[i][j] == 0) {
//New bombs
g2[i][j] = 3;
}
else {
g2[i][j] = 1;
}
}
}
//Explode bombs
int[][] g3 = new int[r][];
for (int i = 0; i < r; i++) {
g3[i] = new int[c];
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
g3[i][j] = g2[i][j] - 1;
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (g2[i][j] == 1) {
//Above
if (i > 0 && g2[i-1][j] != 1) {
g3[i-1][j] = 0;
}
//Below
if (i < r-1 && g2[i+1][j] != 1) {
g3[i+1][j] = 0;
}
//Left
if (j > 0 && g2[i][j-1] != 1) {
g3[i][j-1] = 0;
}
//Right
if (j < c-1 && g2[i][j+1] != 1) {
g3[i][j+1] = 0;
}
g3[i][j] = 0;
}
}
}
int[][] g4 = new int[r][];
for (int i = 0; i < r; i++) {
g4[i] = new int[c];
for (int j = 0; j < c; j++) {
if (g3[i][j] == 0) {
//New bombs
g4[i][j] = 3;
}
else {
g4[i][j] = 1;
}
}
}
//Explode bombs
int[][] g5 = new int[r][];
for (int i = 0; i < r; i++) {
g5[i] = new int[c];
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
g5[i][j] = g4[i][j] - 1;
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (g4[i][j] == 1) {
//Above
if (i > 0 && g4[i-1][j] != 1) {
g5[i-1][j] = 0;
}
//Below
if (i < r-1 && g4[i+1][j] != 1) {
g5[i+1][j] = 0;
}
//Left
if (j > 0 && g4[i][j-1] != 1) {
g5[i][j-1] = 0;
}
//Right
if (j < c-1 && g4[i][j+1] != 1) {
g5[i][j+1] = 0;
}
g5[i][j] = 0;
}
}
}
/*
Console.WriteLine("N = 1");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
Console.Write(g1[i][j]);
}
Console.WriteLine();
}
Console.WriteLine("N = 2");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
Console.Write(g2[i][j]);
}
Console.WriteLine();
}
Console.WriteLine("N = 3");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
Console.Write(g3[i][j]);
}
Console.WriteLine();
}
Console.WriteLine("N = 4");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
Console.Write(g4[i][j]);
}
Console.WriteLine();
}
Console.WriteLine("N = 5");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
Console.Write(g5[i][j]);
}
Console.WriteLine();
}
*/
long N = n;
if (n > 4) {
N = n % 4;
if (N < 2) {
N += 4;
}
}
int[][] gprint = new int[r][];
for (int i = 0; i < r; i++) {
gprint[i] = new int[c];
}
switch(N) {
case 1:
gprint = g1;
break;
case 2:
gprint = g2;
break;
case 3:
gprint = g3;
break;
case 4:
gprint = g4;
break;
case 5:
gprint = g5;
break;
default:
Console.WriteLine("Oops!");
break;
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (gprint[i][j] > 0) {
Console.Write("O");
}
else {
Console.Write(".");
}
}
Console.WriteLine();
}
}
}
The Bomberman Game Java Solution
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);
}
}
The Bomberman Game JavaScript Solution
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function bomberMan(n, grid) {
// Complete this function
const processCells = (action) => {
for (let rn = 0, rl = grid.length; rn < rl; rn++) {
if (typeof grid[rn] === 'string') {
grid[rn] = grid[rn].split('');
}
for (let cn = 0, cl = grid[rn].length; cn < cl; cn++) {
action(grid[rn][cn], rn, cn);
}
}
/*grid.forEach((row, rn) => {
if (!Array.isArray(row)) {
row = grid[rn] = row.split('');
}
row.forEach((cell, cn) => action(cell, rn, cn))
})*/
}
const shouldBeDestroyed = (rn, cn, when) => {
return (grid[rn] && typeof grid[rn][cn] !== 'undefined' && grid[rn][cn] !== when);
}
if (n > 4) n = 4 + n % 4;
for (let i = 0; i <= n; i++) {
if (i === 0) {
processCells((cell, rn, cn) => {
grid[rn][cn] = cell === 'O' ? i : -1;
});
}
if (i > 1 && i % 2 === 0) {
// console.log('fill');
processCells((cell, rn, cn) => {
if (cell === -1) {
grid[rn][cn] = i;
}
});
}
if (i > 1 && i % 2 === 1) {
//console.log('detonate');
processCells((cell, rn, cn) => {
if (cell !== -1 && i - cell === 3) {
grid[rn][cn] = -1;
if (shouldBeDestroyed(rn - 1, cn, cell)) grid[rn - 1][cn] = -1;
if (shouldBeDestroyed(rn + 1, cn, cell)) grid[rn + 1][cn] = -1;
if (shouldBeDestroyed(rn, cn - 1, cell)) grid[rn][cn -1] = -1;
if (shouldBeDestroyed(rn, cn + 1, cell)) grid[rn][cn +1] = -1;
}
})
}
//console.log(grid);
}
let result = grid.map(row => row.map(cell => cell !== -1 ? 'O' : '.').join(''))
return result;
}
function main() {
var r_temp = readLine().split(' ');
var r = parseInt(r_temp[0]);
var c = parseInt(r_temp[1]);
var n = parseInt(r_temp[2]);
var grid = [];
for(var grid_i = 0; grid_i < r; grid_i++){
grid[grid_i] = readLine();
}
var result = bomberMan(n, grid);
console.log(result.join("\n"));
}
The Bomberman Game Python Solution
class Board(object):
def __init__(self, rows, columns):
self.board = [[None for column in range(columns)] for row in range(rows)]
self.explosion_queue = [] # list of tuple positions
def __str__(self):
s = ''
for row in self.board:
s += ''.join([sq.state for sq in row]) + '\n'
return s
def enqueue_explosion(self, position):
self.explosion_queue.append(position)
self.explosion_queue.append((min(position[0]+1, len(self.board)-1), position[1]))
self.explosion_queue.append((max(position[0]-1, 0), position[1]))
self.explosion_queue.append((position[0], min(position[1]+1, len(self.board[0])-1)))
self.explosion_queue.append((position[0], max(position[1]-1, 0)))
def fillBombs(self):
for row in self.board:
for sq in row:
sq.setBomb()
def explode(self):
for pos in set(self.explosion_queue):
self.board[pos[0]][pos[1]].explode()
self.explosion_queue.clear()
def update(self):
for row in self.board:
for sq in row:
sq.tickUp()
if self.explosion_queue:
self.explode()
class Square(object):
def __init__(self, state, row, col, board):
self.state = state
self.ticker = 0
self.position = (row, col)
self.board = board
def resetSquare(self):
self.ticker = 0
def tickUp(self):
if self.state == '.': return
self.ticker += 1
if self.ticker == 3:
board.enqueue_explosion(self.position)
def explode(self):
self.ticker = 0
self.state = '.'
def setBomb(self):
self.state = 'O'
R, C, N = [int(x) for x in input().strip().split(' ')]
board = Board(R, C)
for r in range(R):
s = input().strip()
for c in enumerate(s):
board.board[r][c[0]] = Square(c[1], r, c[0], board)
t = 1
N = min(N, (N % 4) + 4)
while t <= N:
board.update()
if t % 2 == 0: board.fillBombs()
t += 1
print(board)
Other Solutions
Стильные заметки по созданию необычных образов на любой день.
Мнения экспертов, события, все новинки и мероприятия.
https://luxe-moda.ru/chic/499-10-maloizvestnyh-faktov-o-demne-gvasalii/