Please explain the question and code both in details so that its easy to understand the question and its code as well
Problem in understanding concept
@abhishekpandeycsaiml21_c74cb0ea3a791122 Some Points to ponder before discussing the Solution :
- Start from the 0-0 index of the 2 - D matrix( No step would be count hence, strength remain intact).
- Changing row wonβt take any strength.
- If at any time, the Strength become smaller than K ( threshold for piyush), then no need to traverse rest of the array, you can say no nothing else.
Algorithm
- Take input N, M, K, S.
- Take input 2 - D matrix of size N x M.
- Put a loop on the array starting from 0 - 0 index to (N - 1) - (M - 1).
- check if the Strength is lower than the threshold viz, K, print βNoβ and return.
- otherwise,
-
if character is β*β, add 5 to the strength.
-
else if, character is β.β , subtract 2 from the strength.
-
else, if character is β#β, break.
-
- If you are not in the last column, decrement strength by 1.
- After the loop, print βYesβ and strength separated by a new line.
// Implementation for piyush and magical park
package Extras;
import java.util.Scanner;
public class help_piyush {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int m = scn.nextInt();
int k = scn.nextInt();
int s = scn.nextInt();
char[][] arr = new char[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = scn.next().charAt(0);
}
}
maze(arr, k, s);
}
public static void maze(char[][] arr, int k, int s) {
boolean flag = false;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
char ch = arr[i][j];
if (s < k) {
flag = true;
break;
}
if (ch == '*') {
s += 5;
} else if (ch == '.') {
s -= 2;
} else {
break;
}
if (j != arr[0].length - 1)
s--;
}
}
if (!flag) {
System.out.println("Yes");
System.out.println(s);
} else {
System.out.println("No");
}
}
}
}