Piyush and magical park

#include
#include
using namespace std;

int main(){
int n,m,k,s;
int i,j,score;
cin>>n>>m>>k>>s;
score = s;
char a[n][m];//={{’.’,’.’,’’,’.’},{’.’,’#’,’.’,’.’},{’’,’’,’.’,’.’},{’.’,’#’,’’,’*’}};;
// read
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cin>>a[i][j];
}
}
//calulate score
for(i=0;i<n;i++){
while(score>k){
for(j=0;j<m;j++){

		if(a[i][j]=='#')
			break;
		else if(a[i][j]=='*')
			score += 5;
		else
			score -= 2;
	}
	if(j==m)
		score -= j-1; // if loop ends it will make it ++
	else
		score -= j;
}}
if(score>=k)
	cout<<"Yes\n"<<score;
else
	cout<<"No";

return 0;

}
why it is not passing all test cases?

hey @tishya_goyal you need to check if score is greater than k at every step since it is mentioned that if at any moment the energy becomes less that or equal to k, piyush will get lost. moreover

        if(j==m)
		score -= j-1; // if loop ends it will make it ++
	else
		score -= j;

This condition appears to be wrong you should only subtract 1 if j != m-1

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.