


Down: (row+1,col)
Down left diagonal: (row+1,col-1)
Down right diagonal: (row+1, col+1)
The first line contains an integer 'T', which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains two Integers 'N' and 'M' where 'N' denotes the number of rows in the given matrix. And 'M' denotes the number of columns in the given matrix.
The next 'N' line of each test case contains 'M' space-separated integers denoting the cell elements.
For each test case/query, print the maximum sum that can be obtained by taking a path as described above.
Output for every test case will be printed in a separate line.
You do not need to print anything. It has already been taken care of.
1 <= T <= 50
1 <= N <= 100
1 <= M <= 100
-10^4 <= matrix[i][j] <= 10^4
Where 'T' is the number of test cases.
Where 'N' is the number of rows in the given matrix, and 'M' is the number of columns in the given matrix.
And, matrix[i][j] denotes the value at (i,j) cell in the matrix.
Time Limit: 1sec
The basic approach is that we are going to explore all possible paths recursively from the first row for each cell and return the maximum sum among all paths. Steps are as follows:
getMaxPathSum(row,col)=matrix[row][col]+max(getMaxPathSum(row+1,col),*max(getMaxPathSum(row+1,col+1),getMaxPathSum(row+1,col-1))If we draw the recursion tree for the recursive solution of Approach-1, we can observe that there is a lot of overlapping subproblems, there is only N*M distinct recursive call. Since the problem has overlapping subproblems so we can solve it more efficiently using memoization.
Let’s say we have “dp” such that:
Dp[row][col], will store the maximum sum which will path from (row, col) to any cell of the last row including (row, col) cells.
Before calling the function for any valid (row, col), we will first check whether we have already a solution corresponding to the (row, col), if we have already a solution then we return the solution else we will solve it with following steps :
getMaxPathSum(row,col)=matrix[row][col]+max(getMaxPathSum(row+1,col),max(getMaxPathSum(row+1,col+1), getMaxPathSum(row+1, col-1) )4. And finally, before returning the answer, we will save it in “dp” for later use.
We are given an N*M matrix. Our idea behind this approach is while traversing in the given matrix, and we will keep updating the matrix with the best sum path till now. That means, we are going to the next row after completing the current row, So for each cell, all the cells from where we can reach here already contain the best path sum from any cell of the first row to that cell. Let’s say we are at (row, col), So there is only three way to reach (row, call):
So, matrix[row][col] will be,
matrix[row][col] = matrix[row][col] + max(matrix[row - 1][col], max(matrix[row - 1][max(0, col - 1)], matrix[row - 1][min(m-1, col + 1)]));Where ‘M’ is the number of columns in the given matrix.
Once we have updated the “matrix” then each cell of the matrix (row, col) will represent the maximum path sum for any cell from the first row to current (row, col) cell. So we will traverse in the last row and find the maximum value that will be the maximum path sum from any cell of the first row to any cell of the last row.