


If the matrix is
0 2 4 1
4 8 3 7
2 3 6 2
9 7 8 3
1 5 9 4
Then answer is 47. As, Alice will collect coins 0+8+3+9+1 = 21 coins. Bob will collect coins 1+7+6+8+4 = 26 coins. Total coins is 21+26 = 47 coins.
The first line of input contains a single integer T, representing the number of test cases or queries to be run.
Then the T test cases follow.
The first line of input contains two single space-separated integers 'N' and 'M' representing the number of rows and columns of the matrix respectively.
The next 'N' lines contain 'M' single space-separated integers each representing the coins in a row of the matrix.
The only line of output contains a single integer i.e. The minimum coins that can be collected by Alice and Bob.
You are not required to print the expected output, it has already been taken care of. Just implement the function.
1 <= T <= 5
1 <= N <= 10^2
1 <= M <= 10^2
0 <= MAT[i][j] <= 10^3
Time Limit: 1 sec
The idea is to do both traversals concurrently. We start first from (0, 0) and second traversal from (0, m-1) simultaneously. The important thing to note is, at any particular step both traversals will be in the same row as in all possible three moves, row number is increased. Since variation in y could occur in 3 ways i.e bottom, bottom right, bottom right. So in total 9 combinations are possible among y coordinates.
Approach:
The idea is to do both traversals concurrently. We start first from (0, 0) and second traversal from (0, m-1) simultaneously. The important thing to note is, at any particular step both traversals will be in the same row as in all possible three moves, row number is increased. Since variation in y could occur in 3 ways i.e bottom, bottom right, bottom right. So in total 9 combinations are possible among y coordinates.
Approach:
The idea is to do both traversals concurrently. We start first from (0, 0) and second traversal from (0, m-1) simultaneously. The important thing to note is, at any particular step both traversals will be in the same row as in all possible three moves, the row number is increased. Since variation in y could occur in 3 ways i.e bottom, bottom right, bottom right. So in total 9 combinations are possible among y coordinates.
Approach:
The idea is to do both traversals concurrently. We start first from (0, 0) and second traversal from (0, m-1) simultaneously. The important thing to note is, at any particular step both traversals will be in the same row as in all possible three moves, the row number is increased. Since variation in y could occur in 3 ways i.e bottom, bottom right, bottom right. So in total 9 combinations are possible among y coordinates.
Approach:
Since maximum coins that are collected at any row ith will only depend on the number of coins that are collected at (i-1)th row, thus instead of using 3d array of size N*M*M we will use two 3d arrays of size 2*M*M with two variables (CURR and PREV) where to curr will store the answer of the ith row and PREV will store the answer of (i-1)th row, Now, if we will be at (i+1)th row then CURR array will become PREV array.