why it is giving 13 as the ans, as when we are getting in new line for the 3rd row we are geeting greater ans then 13, please explain me sir
Piyush magical park
sample case:
4 4 5 20
. . * .
. # . .
* * . .
. # * *
S=20 and k=5;
first row:
20-2-1-2-1+5-1-2=16
second row:
16-2=14 (Now, he cannot move any further)
third row: -1 for changing the row
14-1+5-1+5-1-2-1-2=16
fourth Row:
16-2-1=13
As 13 >=5, Print “Yes” and 13
please tell me the error
you are not taking input correctly for a 2d array and dont fix the value of energy to 20…
//code to take 2d array input
for(int i =0;i<m;i++)
{
String str=sc.next();
for(int j =0;j<n;j++)
{
a[i][j]=str.charAt(j);
}
}
import java.util.; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); int n = sc.nextInt(); int k= sc.nextInt(); int s = sc.nextInt(); char a[][] =new char[m][n]; for(int i =0;i<m;i++) { for(int j =0;j<n;j++) { a[i][j]=sc.next().charAt(0); } } int energy =s; for(int i=0;i<m;i++) { for(int j =0;j<n;j++) { if(a[i][j]==’.’) { energy-=2; }else if(a[i][j]==’’) { energy+=5; }else if(a[i][j]==’#’) { break; } if(i!=m-1 && j!=n-1) { energy–; } } } if(energy>=k) { System.out.println(“Yes”); System.out.println(energy); }else { System.out.println(“No”); } } }
take the input as suggested by me in the comments…
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.