I just have some doubt in the logic of the code.
Please refer to the code below , my doubt is that suppose while traversing the first row(iteration i=1)
‘s’ becomes less than ‘k’ thus the if(s<k) condition will be true and the control would break from the inner loop and thus i will become 2. Now when i=2 still in the memory s<k is true and thus when
The if(s<k) condition is encountered again the inner loop would break again and this will keep on happening for the remaining values of i without even traversing all the rows which might have made s>k . This will ulimately give us the output NO
#include
using namespace std;
void magical_park(char a[][100],int m,int n,int k,int s)
{
bool success=true;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
char ch=a[i][j];
if(s<k){
success=false;
break;
}
if(ch=='*'){
s=s+5;
}
else if(ch=='.'){
s=s-2;
}
else {
break;
}
if(j!=n-1){
s--;
}
}
}
if(success==true){
cout<<"Yes"<<endl;
cout<<s;
}
else{
cout<<"No"<<endl;
}
}
int main(){
int m,n,k,s;
cin>>m>>n>>k>>s;
char park[100][100];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>park[i][j];
}
}
magical_park(park,m,n,k,s);
return 0;
}