


Input: ‘R’ = 3, ‘C’ = 4
‘GRID’ = [[2, 3, 1, 2], [3, 4, 2, 2], [5, 6, 3, 5]]
Output: 21
Initially Alice is at the position (0,0) he can follow the path (0,0) -> (1,1) -> (2,1) and will collect 2 + 4 + 6 = 12 chocolates.
Initially Bob is at the position (0, 3) and he can follow the path (0, 3) -> (1,3) -> (2, 3) and will colllect 2 + 2 + 5 = 9 chocolates.
Hence the total number of chocolates collected will be 12 + 9 = 21. there is no other possible way to collect a greater number of chocolates than 21.
The first line of input contains an integer 'T', denoting the number of test cases.
For each test case, the first line contains two integers' R' and 'C' denoting the number of rows and number of columns in the grid.
Next 'R' lines will contain 'C' integers each the value of each cell in the grid.
For each test case, print the maximum number of chocolates that can be collected.
Output for each test case will be printed in a separate line.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
2 <= 'R', 'C' <= 50
0 <= 'GRID[i][j]'<= 10^2
Time Limit: 1sec
Each person has three choices in the next row, so combined, they have 3 * 3 = 9 choices. Among these nine choices, we will consider the path which gives us the maximum score. In case both of them land on the same cell in a row, we will add that cell's value only once.
We will implement the above logic recursively.
Algorithm :
We will use dynamic programming, and 'dp[R][C1][C2]' will maintain the maximum total score that both robots can accumulate till the end when starting from columns 'C1' and 'C2' of row 'R'.
We will optimize it more by skipping 'C1' > 'C2' paths because scores of those paths would already be counted in symmetrical 'C1' < 'C2' paths.
Algorithm :
We can also solve this problem using iterative dynamic programming. The approach is similar to the recursive approach with a slight change. We will start from the base conditions instead of the other way around. The idea is to keep only the previous row dp array in memory and construct the current row dp array thanks to it. Then when the current loop is finished, we just have to assign the previous row dp array to be the current row dp array.