| 5 | using namespace std; |
| 6 | |
| 7 | int minSum(int currRow,int currCol,int m,int n,vector<vector<int>>&matrix, vector<vector<int>>&v) |
| 8 | { |
| 9 | //base conditions |
| 10 | if(currCol < 0 || currCol >= n) return 100001; |
| 11 | if(currRow == m-1) return matrix[currRow][currCol]; |
| 12 | |
| 13 | //if value is already present in the vector v ,then extract from it, no need to make recursive calls for it |
| 14 | if(v[currRow][currCol]!=-1) return v[currRow][currCol]; |
| 15 | |
| 16 | //exploring the three possibilities :-1.Left Diagonal movement, 2.downward movement, 3.Right movement |
| 17 | int leftD = matrix[currRow][currCol] + minSum(currRow+1,currCol-1,m,n,matrix,v);//moving Diagonally Left |
| 18 | int down = matrix[currRow][currCol] + minSum(currRow+1,currCol,m,n,matrix,v); //moving Downward |
| 19 | int rightD = matrix[currRow][currCol] + minSum(currRow+1,currCol+1,m,n,matrix,v);//moving Diagonally Right |
| 20 | |
| 21 | //storing the answer for the current coordinate in v,so that we can use it in future |
| 22 | v[currRow][currCol] = min(down,min(leftD,rightD)); |
| 23 | return v[currRow][currCol]; |
| 24 | } |
| 25 | int minFallingPathSum(vector<vector<int>>& matrix) |
| 26 | { |
| 27 | int m = matrix.size(); //total number of rows |