#include <iostream>
#include <string>
using namespace std;
bool magic(string park[], int n, int m, int k, int s)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{ if (s<k)
{
return false;
}
if (park[i][j] == '.')
{
s = s - 2;
}
else
{
if (park[i][j] == '*')
{
s = s + 5;
}
else
{
break;
}
}
if (j!=m-1)
{
s=s-1;
}
}
}
cout<<s;
return true;
}
int main()
{
int n, m;
cin >> n >> m;
int k, s;
cin >> k >> s;
string park[100];
for (int i = 0; i < n; i++)
{
getline(cin, park[i]);
}
bool ans= magic(park, n, m, k, s);
cout<<boolalpha<<ans;
return 0;
}
Why my code is not generating correct output
are you there?
to solve the query
int n, m;
cin >> n >> m;
int k, s;
cin >> k >> s;
after this you have to remove ‘\n’ which is also present on stream
otherwise it will be taken by getline(cin, park[i]);
for that just use cin.ignore()
still the output generated is not corrrect
4 4 5 20
. . * .
. # . .
-
- . .
. # * *
output should be 13 true but it is printing 15 true
- . .
input also contain spaces between every two character which don’t consider
so best way is to take input using cin which ignore whitespaces and store array in char array
Thanks allot saurabh for putting your valuable time into clearing doubt 
#include<iostream> using namespace std; bool magic(char park[][100],int n,int m,int s, int k) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s<k) { return false; } if (park[i][j]=='.') { s-=2; } else { if (park[i][j]=='*') { s+=5; } else { break; } } if (j!=m-1) { s-=1; } } } return true; } int main(){ int n,m,s,k; cin>>n>>m>>s>>k; char park[100][100]; cin.ignore(); for (int i = 0; i < n; i++) { for (int j = 0; j <m; j++) { cin>>park[i][j]; } } bool ans=magic(park,n,m,s,k); if (ans) { cout<<"Yes"; cout<<s; } else { cout<<"NO"; } return 0; }
whats wrong now?
#include<iostream>
using namespace std;
bool magic(char park[][100],int n,int m,int s, int k)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (s<k)
{
return false;
}
if (park[i][j]=='.')
{
s-=2;
}
else
{
if (park[i][j]=='*')
{
s+=5;
}
else
{
break;
}
}
if (j!=m-1)
{
s-=1;
}
}
}
return true;
}
int main(){
int n,m,s,k;
cin>>n>>m>>s>>k;
char park[100][100];
cin.ignore();
for (int i = 0; i < n; i++)
{
for (int j = 0; j <m; j++)
{
cin>>park[i][j];
}
}
bool ans=magic(park,n,m,s,k);
if (ans)
{
cout<<"Yes";
cout<<s;
}
else
{
cout<<"NO";
}
return 0;
}
Correct ouput not generated
ohh… got it can you tell how can i print modified s in main as it is printing 20
done not all test cases are passing 
@sagar_aggarwal see the output format carefully, you have to print value of s in next line, you are doing so in the same line
did that too…still not all test cases are passing