We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
// Complete the bomberMan function below.
static String[] bomberMan(int n, String[] grid) {
String[] ans = new String[grid.length];
int aa = grid[0].length();
String ab = "";
//String[] grd = new String(grid.length);
for(int i=0;i<aa;i++){
ab = ab.concat("0");
}
if(n%2 == 0){
for(int i=0;i<grid.length;i++){
ans[i] = ab;
}
}
else{
if(n==1){
ans = grid;
}
else{
int count = 0;
while(count<2){
for(int i=0;i<grid.length;i++){
char[] tmp1 = grid[i].toCharArray();
for(int j=0;j<aa;j++){
if(tmp1[j] == 'O'){
tmp1[j] = '1';
}
else if(tmp1[j] == '.'){
tmp1[j] = 'O';
}
}
String ala = new String(tmp1);
grid[i]=ala;
}
int flag = 0;
int flag1 = 0;
for(int i=0;i<grid.length;i++){
char[] tmp = grid[i].toCharArray();
char[] tm2 = grid[i].toCharArray();
char[] tm3 = grid[i].toCharArray();
if(i-1 != -1){
tm2 = grid[i-1].toCharArray();
}
else{
flag = 1;
}
if(i+1 != grid.length){
tm3 = grid[i+1].toCharArray();
}
else{
flag1 = 1;
}
for(int j=0;j<aa;j++){
if(flag == 0 && flag1 == 0){
if(tmp[j] == '1'){
tmp[j] = '.';
if(j!=0){
tmp[j-1] = '.';
}
if(j!=tmp.length-1){
if(tmp[j+1] == 'O'){
tmp[j+1] = '.';
}
}
if(tm3[j] == 'O'){
tm3[j] = '.';
}
if(tm2[j] == 'O'){
tm2[j] = '.';
}
}
}
else if(flag == 1 && flag1 == 0){
//flag = 0;
if(tmp[j] == '1'){
tmp[j]='.';
if(j!=0){
tmp[j-1] = '.';
}
if(j!=tmp.length-1){
if(tmp[j+1] == 'O'){
tmp[j+1] = '.';
}
}
if(tm3[j] == 'O'){
tm3[j] = '.';
}
}
}
else if(flag1 == 1 && flag == 0){
//flag1 = 0;
if(tmp[j] == '1'){
tmp[j]='.';
if(j!=0){
tmp[j-1] = '.';
}
if(j!=tmp.length-1){
if(tmp[j+1] == 'O'){
tmp[j+1] = '.';
}
}
if(tm2[j] == 'O'){
tm2[j] = '.';
}
}
}
else{
if(tmp[j] == '1'){
tmp[j]='.';
if(j!=0){
tmp[j-1] = '.';
}
if(j!=tmp.length-1){
if(tmp[j+1] == 'O'){
tmp[j+1] = '.';
}
}
}
}
}
String a3 = new String(tmp);
grid[i] = a3;
if(flag == 0 && flag1 == 0){
String a1 = new String(tm2);
String a2 = new String(tm3);
grid[i-1] = a1;
grid[i+1] = a2;
}
else if(flag == 1 && flag1 == 0){
//String a1 = new String(tm2);
String a2 = new String(tm3);
grid[i+1] = a2;
flag = 0;
}
else if(flag == 0 && flag1 == 1){
String a1 = new String(tm2);
//String a2 = new String(tm3);
grid[i-1] = a1;
flag1 = 0;
}
}
count++;
ans = grid;
if((n+1)%4==0){
//grd = grid;
break;
}
}
}
}
return(ans);
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] rcn = scanner.nextLine().split(" ");
int r = Integer.parseInt(rcn[0]);
int c = Integer.parseInt(rcn[1]);
int n = Integer.parseInt(rcn[2]);
String[] grid = new String[r];
for (int i = 0; i < r; i++) {
String gridItem = scanner.nextLine();
grid[i] = gridItem;
}
String[] result = bomberMan(n, grid);
for (int i = 0; i < result.length; i++) {
bufferedWriter.write(result[i]);
if (i != result.length - 1) {
bufferedWriter.write("\n");
}
}
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}
Abhiskek
The pattern is like if n=1 ans = grid
if n%2 == 0 than all are 0's
now the headache is when it is other than above cases
I. For all cases the solution for n = 3,7,11,15....(i.e. the configration of grid at n=3) is same while for n = 5,9,13.... it is same as that of initial i.e. at t=1.
II. While for the second case it is like, for n = 3,7,11,15.... the solution is still same as that of the n=3 while for n = 5,9,13.... it is different from that of t=1 or initital. But for all n E (5,9,13,17...) it is same. So for worst case you need to follow the pattern mentioned(i.e. {i,j} = 0 will affect {i+-1 and j+-1}) in question for 2 times i.e. first time to find the grid for n=3,7,11... and second time for n=5,9,13,17... !!
I am not able to figure out for what cases of n=5,9,13,17..... we have to use I and for what cases II.
Hope it will be helpful for u.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Bomberman Game
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.regex.*;
public class Solution {
}
Abhiskek The pattern is like if n=1 ans = grid if n%2 == 0 than all are 0's now the headache is when it is other than above cases I. For all cases the solution for n = 3,7,11,15....(i.e. the configration of grid at n=3) is same while for n = 5,9,13.... it is same as that of initial i.e. at t=1.
II. While for the second case it is like, for n = 3,7,11,15.... the solution is still same as that of the n=3 while for n = 5,9,13.... it is different from that of t=1 or initital. But for all n E (5,9,13,17...) it is same. So for worst case you need to follow the pattern mentioned(i.e. {i,j} = 0 will affect {i+-1 and j+-1}) in question for 2 times i.e. first time to find the grid for n=3,7,11... and second time for n=5,9,13,17... !!
I am not able to figure out for what cases of n=5,9,13,17..... we have to use I and for what cases II.
Hope it will be helpful for u.