Cpp solution wether we can reach the right bottom or not


#1

Comment body goes here.


#2

int Solution::solve(vector<vector > &A) {

//dp[i][j] will tell you the length of the path from 0,0 to i,j;

int n = A.size();
int m = A[0].size();

 int dp[n][m];
 
 memset(dp,-1,sizeof(dp));
 
 dp[0][0] = 1;
 
 for(int row=1;row<n;row++)
 {
     if(A[row][0] < A[row-1][0])
      break;
     else
      dp[row][0] = dp[row-1][0]+1;
 }
 
 for(int col=1;col<m;col++)
 {
     if(A[0][col] < A[0][col-1])
      break;
     else
      dp[0][col] = dp[0][col-1]+1;
 }
 
 for(int i=1;i<n;i++)
 {
     for(int j=1;j<m;j++)
     {
         if(dp[i][j-1]==-1 && dp[i][j-1]==-1)
         {
             return -1;
         }
         int temp1 = -1 ,temp2 = -1;
         if(dp[i-1][j]!=-1 && A[i-1][j] < A[i][j])
         {
             temp1 = dp[i-1][j]+1;
         }
         if(dp[i][j-1]!=-1 && A[i][j-1] < A[i][j])
         {
             temp2 = dp[i][j-1]+1;
         }
         
         dp[i][j] = max(temp1 ,temp2);
     }
 }

return dp[n-1][m-1];

}