Ninja Hire 4th year, round 2

Round 2 is over, we can discuss now.

Can anybody tell me the approach and code behind MATRIX GYM problem ?

My code was giving segmentation fault for all cases including sample TC.

My Code

#include<bits/stdc++.h>
using namespace std;

int main(){

int n;
cin>>n;

string str;
cin>>str;
int m=str.size();

bool mat[n][m];

for(int i=0;i<str.size();i++){
    if(str[i] == '#') mat[0][i] = false;
    else mat[0][i] = true;
}

for(int i=1;i<n; i++){
    
    cin>>str;
    int t=(int)str.size();
    for(int j=0; j<min(m,t); j++){
        if(str[j] == '#') mat[i][j] = false;
    	else mat[i][j] = true;
    }
    
}



int target;
cin>>target;

int dp[2][m];


if(n==1) {
    bool flag=false;
    for(int i=0; i<str.size(); i++){
        if(str[i] == '#'){ flag=true; break;} 
    }
    if(flag) cout<<"No"; 
    else {
    	int got= 5*(str.size() - 1 ); 
        int temp=got - target;
        if(temp == 0) cout<<"Yes";//<<temp;
        else if(temp > 0) cout<<"Yes "<<temp;
        else cout<<"No";
    }
    //return 0;
}else{

dp[0][0]=1;    

    for(int j=1;j<m;j++){
        
        if(mat[0][j]) dp[0][j] = 1+dp[0][j-1];
        else dp[0][j] = INT_MIN;
        
    }


for(int i=1; i<n; i++){
    
    for(int j=0;i<m;j++){
        if(!mat[i][j]) dp[1][j] = INT_MIN;
        else dp[1][j] = 1 ;
    }
    
    for(int j=0;j<m;j++){
        if(dp[1][j] == 1){
            if(j>0){
            	if(dp[0][j] == INT_MIN && dp[1][j-1] == INT_MIN) 
                	dp[1][j] = INT_MIN;
            	else
                	dp[1][j] = max(dp[0][j], dp[1][j-1]) + 1;    
            }else{
                if(dp[0][j] == INT_MIN) 
                	dp[1][j] = INT_MIN;
            	else
                	dp[1][j] =dp[0][j] + 1;
            }            	
        }
        
    }
    
    for(int j=0;j<m;j++)
    	dp[0][j] = dp[1][j];
}
    /*
    for(int i=0;i<2;i++)
	{
    for(int j=0;j<m;j++)
        cout<<dp[i][j]<<" ";
    
    cout<<endl;
	}
	 return 0;
	*/

int got = 5 * (dp[0][m-1] - 1);
int temp=got - target;
if(temp == 0) cout<<"Yes";//<<temp;
else if(temp > 0) cout<<"Yes "<<temp;
else cout<<"No";      

}
return 0;

}

Can you post screen shot of the question?

Please post the question.

Bhai bina questions k kaise samjh me aayega? You can share the question here @jk2365

@jk2365 You were getting segmentation fault because you took the input in a single string but there were ā€˜nā€™ separate strings (one for each row).