Simple recusion. No DP. No BFS. Passes everything


#1
indent preformatted text by 4 spaces

bool path;
void rec(vector<vector> &A, int i, int j){
int m = A.size();
int n = A[0].size();

if(i==m-1 && j==n-1){
    path = true;
    return;
}
if(i+1<m && A[i+1][j]>A[i][j]) rec(A, i+1, j);

if(!path)
    if(j+1<n && A[i][j+1]>A[i][j]) 
        rec(A, i, j+1);

}

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

path = false;
int m = A.size();
int n = A[0].size();
rec(A, 0, 0);

if(path) return m+n-1;
else return -1;

}