Can you please tell me what am i not considering?

import java.util.*;
public class Main {
public static void main(String args[]) {
// Your Code Here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
int s = sc.nextInt();
sc.nextLine();
char[][] a = new char[n][m];

	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			a[i][j] =sc.next().charAt(0);
		}
	}
	boolean flag=true;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(j!=0)
			{
				s--;
			}
			if(s<=k)
			{
		
				break;
			}
			if(a[i][j]=='.')
			{
				s-=2;
					
			}
			if(a[i][j]=='*')
			{
				s+=5;
				
				
			}
			if(a[i][j]=='#')
			{
				
				break;
			}
			

		}
		if(s<=k)
		{
			flag=false;
			break;
		}
	}

	if(flag)
	{
		System.out.println("Yes");
	}
	else{
		System.out.println("No");
	}
	System.out.println(s);

}

}

Two mistakes: Firstly, you don’t need to check the condition s <= k while traversing the array. It may be possible that he has less than k strength at some point but the final strength when leaving the maze is greater than or equal to k. In such case, your code fails. And when answer is yes, only then print the final strength.

1 Like