How I Solved Bomberman Challenge On Hackerrank, The Bomberman Game Discussions


Today is not a nice day in the Twin Cities of Minneapolis and St. Paul. It rained all night long and will continue to do so until late evening. Currently the temperature at 44 F and will continue to drop a few degrees. It is a perfect day to make and bake pizza.

Đang xem: How i solved bomberman challenge on hackerrank

After breakfast I made the dough from scratch. It is important to have quality high gluten flower and yeast. The dough is now sitting in a bowl in the kitchen covered with a towel letting it rise. Apparently we are missing red onions and green peppers. My wife and I will stop by Hy-Vee around noon to get the missing ingredients. We are making thick pepperoni pizzas.

Yesterday my wife and I stopped by my son’s house in the Twin Cities. They


were celebrating the birthdays of his two sons with family and friends. It was cold and very windy. That said; my son managed to grill hot-dogs and burgers. It was a pleasant afternoon.

The topic for this post is the HackerRank challenge The Bomberman Game. I thought it was interesting and harder that suggested. If interested, read the requirements, and think about how you would approach the solution. Then come back to this post.

It seems that there are four steps. The first two are the initial ones. The last two repeat as long as required by each test case.

I tried different approaches to implement the fuse. It starts with a value of 3 seconds and decrements once per second. During the simulation cycle, two additional events must be taken care of. The Bomberman plants fresh bombs on open grid locations and the bombs need to detonate 3 seconds after they were planted.

The final diagram illustrates what I got out of the requirements:

decrement fuseplant nothing plant detonate bombsinitial bombs bombs0 ——– 1 ——– 2 ——– 3 ——–> time in sec ^ v | | +———-+I decide to use char arrays holding the values given the fact that strings are immutable. I used binary values 3, 2, 1, and 0 but later decided to switch to their ASCII representations due to the fact that they were easier to print.

I used the following test case with different counts to test what I considered a solution candidate.

Please disregard the annotations at this time and concentrate on the result for each test.

6 7 0……….O…….O………OO…..OO……………O…….O………OO…..OO…..6 7 1……….O…….O………OO…..OO……..O…….O………OO…..OO…..Some few cases past the first submission but most timed out. I had to optimize the code. After spending some time decided to purchase a test case. I believe it was # 11.

Take a look at the first 3 numbers in this test case:

1 199 181054341O..OO……..O..O……..OO.O.OO.OO…O…..OOO…OO.O..OOOOO…O.O..O..O.O..OOO..O..O..O….O…O….O…O..O..O….O.O.O.O…..O…..OOOO..O……O.O…..OOO….OO….OO….O.O…O..OO….OO..O…OThe single string with a length of 199 values had to be updated 181,054,341 one hundred and eighty one million times. That would take a long time so what could be done?


Take a look at the first few results using the sample input and let’s see if we can determine a pattern. The reason for this is the fact that the first two steps are initialization and the rest repeat.

The edited screen capture seems to have a pattern. All even cases for n are grids with ‘O’s. That is easy to figure without having to simulate 181,054,341 seconds. But wait, the problem is that the count is even.

After further looking, it seems that there are two additional repeating patterns. I flagged them a pattern3 and pattern5 respectively. The end of the tunnel seems near.

The number of counts (n) is associated with when these last two patterns appear. After a couple minutes we can figure the following:

3 + 4 + 4 + 4 …and5 + 4 + 4 + 4 …Now you can see why I named one frequency3 and the other frequency5. We could write some Java code to determine in which case a large value of n falls into. In the additional test case it seems that it falls into test case 5:

Instead of looping millions of times, we can perform some calculations, generate the first few patters and then jump to the result. The approach is illustrated in the following Java code written using the Eclipse IDE:

/* * Complete the bomberMan function below. */ static String<> bomberMan(int n, // the number of seconds to simulate String<> grid) {// array of strings that represents the grid // **** build initial grid of characters **** char<><> g = initialGrid(grid); // **** compute the number for the final grid **** int fg = finalGrid(n); // **** to save intermediate grids (as needed) **** char<><> g2 = null; char<><> g3 = null; char<><> g5 = null; // **** set the number of cycles to run **** int maxCycle = n; if (n >= 5) maxCycle = 5; // ***** run first few cycles in the simulation **** for (int cycle = 2 ; cycle We start by building the initial grid using a two dimensional char array.

As we discussed earlier, we need to determine which final pattern we need to use. The finalGrid() method returns such information.

The next step is to declare placeholders for the 3 patterns we will need. The first one g2 is for even number of cycles, the g3 and g5 hold respectively pattern3 and pattern5.

After determining the number of cycles we need to execute in order to generate set of three patterns (g2, g3 and g5) we run the first few cycles of the simulation.

Xem thêm: Địa Điểm Du Lịch Miền Tây Sông Nước Tự Túc, Kinh Nghiệm Du Lịch Miền Tây Sông Nước Tự Túc

We decrement the fuse count by one (second), on even number cycles the


Bomberman plants bombs with fresh fuses and finally on every loop we need to decrement the fuse so the bombs can detonate.

The last few lines in the loop allow us to store the three grids of interest.

Note that we only cycle at most five times. When done with the loop we determine if we use the current grid or one of the saved patterns. After converting the grid of characters into an array of strings we return our solution.

Let’s go briefly over each method in the solution.

/* * Generate initial grid as char<><> */static char<><> initialGrid(String<> grid) {// **** **** int rows = grid.length; // **** ****char<><> g = new char<>;// **** ****for (int r = 0; r This method takes the initial array of strings with ‘.’s and ‘’O’s and returns a two dimensional array of chars with ‘.’s for places without a bomb and ‘2’s for places with a bomb. Note that we use character ‘2’ instead of character ‘3’ because the second step is to delay for one second. We could have started with ‘3’s and then decrement them by one to end up with ‘2’s.

/* * */static char<><> cloneArray(char<><> g) {// **** ****int rows = g.length;int cols = g<0>.length;// **** ****char<><> gc = new char;// **** ****for (int r = 0; r There are a few ways to clone an array. I tried a couple (e.g., System.arraycopy() and array.clone()) but they did not seem to work like I expected, so I decided to implement this method.

The next method is used to decrement by one the fuse count:

/* * Decrement fuse count */static char<><> decrementFuses(char<><> g) {// **** ****int rows = g.length;int cols = g<0>.length; // **** traverse the grid decrementing fuse counts ****for (int r = 0; r The idea is quite simple. Traverse the grid of characters and replace ‘3’ with ‘2’, ‘2’ with ‘1’ and ‘1’ with ‘0’. Like I previously stated I could have used ‘03’, 02’, ‘01’ and ‘00’ but I was displaying the values and preferred to use the ASCII equivalents.

I believe you can just decrement a char value by one, but decided to use if statements instead. If I would have written this code in C / C++ I would have used binary values instead of ASCII.

/* * Detonate bombs */static char<><> detonateBombs(char<><> g) {// **** ****int rows = g.length;int cols = g<0>.length;// **** copy the grid ****char<><> cg = cloneArray(g);// **** traverse the grid detonating bombs ****for (int r = 0; r 0)g = “.”;// **** down ****if (r 0)g = “.”;// **** center ***g = “.”;}}}// **** returned the cloned grid ****return g;}Note that when a bomb detonates, it destroys the top, right, bottom and left bombs and of course itself. The issue we have is not to disturb other bombs in the vicinity which need to detonate as we traverse the grids. Because of this issue, we need to operate of a copy of the grid.

After a copy of the grid is made, we check for bombs in the original grid, but need to mark its effects in a separate grid. The only location we set to ‘.’ is the location for the bomb that has detonated.

/* * plant bombs with 3-second fuse */static char<><> plantBombs(char<><> g) {// **** ****int rows = g.length;int cols = g<0>.length;// **** traverse the grid ****for (int r = 0; r This method plants bombs in all locations in the grid which do not have one. We replace the ‘.’ with a character ‘3’ to indicate a bomb with a full fuse.

/* * Generate string array from char<><> grid */static String<> charToStringArray(char<><> g) {// **** number of rows in the grid ****int rows = g.length;// **** allocate the string grid ****String<> grid = new String;// **** make a copy of the input grid ****char<><> copy = cloneArray(g);// **** generate strings for the grid ****for (int r = 0; r = “1” && copy This method is used to generate an array of strings from the character array. Note that in order not to disturb the grid, we use a clone.

/* * Return the number for the final grid */static int finalGrid(int n) {// ???? ????System.out.println(“finalGrid This method is used to determine which pattern will be used as a returned value when the simulation ends; otherwise we would have to run all the steps in the simulation which would take too much iteration. Note that the sample input uses 3 seconds.

The entire code for the solution follows:

import*;import java.math.*;import*;import java.text.*;import java.util.*;import java.util.concurrent.*;import java.util.regex.*;/* * */public class Solution {/* * Generate initial grid as char<><> */static char<><> initialGrid(String<> grid) {// **** **** int rows = grid.length; // **** ****char<><> g = new char<>;// **** ****for (int r = 0; r 0)g = “.”;// **** down ****if (r 0)g = “.”;// **** center ***g = “.”;}}}// **** returned the cloned grid ****return g;}/* * plant bombs with 3-second fuse */static char<><> plantBombs(char<><> g) {// **** ****int rows = g.length;int cols = g<0>.length;// **** traverse the grid ****for (int r = 0; r = “1” && copy = 5) maxCycle = 5; // ***** run first few cycles in the simulation **** for (int cycle = 2 ; cycle Please note that I edited the test scaffolding so it could cycle producing multiple results. Such code has been commented out.

If you are interested in the actual code you can find it in my GitHub repository.

Hope you enjoyed this challenge. I did.

Xem thêm: whites coinmaster pro

If you have comments or questions regarding this or any other post in this blog, or if you would like me to help with any phase in the SDLC (Software Development Life Cycle) of a product or service, please do not hesitate and leave me a note below. Requests for help will remain private.

Leave a comment

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