Piyush && magical park

https://hack.codingblocks.com/contests/c/537/55?_ga=2.95358108.1840443150.1548239118-665357574.1546102235
Please explains the test case how it is coming 13.
4 4 5 20
. . * .
. # . .

    • . .
      . # * *

-2 -2 +5 -2
-2 #
As this is blocked how to move forward?

In this question, it is mentioned that
“If he encounters ‘#’, this means that the row is blcked and he cannot move forward.”
This means that he cannot move forward in that row, but he’ll move to the next row on encountering ‘#’.

So in case of sample input
4 4 5 20
. . * .
. # . .

    • . .
      . # * *

The dry run would be

Row Col Strength
0 0 17
0 1 14
0 2 18
0 3 16
1 0 13
2 0 17
2 1 21
2 2 18
2 3 16
3 0 13

Hope you get it…
Now think over the question again. :slightly_smiling_face:

yes understood sir, thanks

Initial S = 20
one encounter of “.” = -3 energy ( 2 because of “.” 1 energy used in traversing )
In 4 th iteration traversing from col 2 to 3 Strength should go from 18 to 15 please check.
1 energy is traversing and 2 energy because of “.” = -3

import java.util.;
public class PiyushPark {
public static void main(String args[])
{
Scanner sr = new Scanner(System.in);
int M,N,K,S;
M = sr.nextInt();
N = sr.nextInt();
K= sr.nextInt();
S = sr.nextInt();
char str[][] = new char[M][N];
// String str = sr.nextLine();
for(int i =0 ; i< M; i++)
{
for(int j =0 ; j < N; j++)
{
str[i][j] = sr.next().charAt(0);
}
}
boolean flag = true;
int energy = S;
for(int i =0 ; i< M && flag == true; i++)
{
for(int j =0 ; j < N && flag == true; j++)
{
if(str[i][j]==’#’)
{
if(energy >= K)
{
System.out.print(“Yes\n”+energy);
flag = false;
break;
}
else if( energy < K)
{
System.out.println(“No”);
flag = false;
break;
}
}
else if(str[i][j]==’.’)
{
energy = energy-3;
if(energy <=0)
{
System.out.println(“No”);
flag= false;
break;
}
}
else if(str[i][j]==’
’)
{
energy = energy+5;
}
}
}

	/*This prints the char array */

// for(int i =0 ; i< M; i++)
// {
// for(int j =0 ; j < l ; j++)
// {
// System.out.print(str[i][j]);
// }
// System.out.println();
// }
}

}

This code fails the third condition can you suggest what it could be .

and secondly we only get 13 at column 2 row 2 if we dont substract 1 when we encounter *

I have the same doubt…if you had done this please explain me too

Hi Abhinav,in this question when you encounter ‘.’ you have to reduce the energy by 2 and secondly whenever you move forward the energy is reduced by 1 so for that you can put the condition that if(j<m-1) then you have to do energy= energy-1 for every iteration and dont print yes or no inside the loop,for this you can put a condition outside the loops that if energy>k then print yes and energy else print No. Also one more thing your matrix is NxM.

This is the correct version which i believe is true. Updations are mainly due to the fact that while moving from last of one row to first of another no extra cost will be incurred.

sir I tried to solve this problem by two ways :-

  1. approach as the questions says
  2. in this one instead of working with last element of row and then moving to first element of next row
    and do (S-2) I just did (S-2) for every last element in a row
    here second approach seems to pass all the test cases and in 1’st one the test case 2 seems to fail
    help me with this is possible please provide second test case that will help me understand this better