Given a matrix grid[][] of order N*M with numbers 0-9 in the cells. The task is to find the maximum amount of money collected when two persons move from (0, 0) to (N-1, M-1) by moving only right and down. If both persons are at the same cell, then only one of them can pick the money at that location.
Examples:
Input: 1 1 1 1 0 1 1 1 1 Output: 8 Explanation: Let 1 denote the places where person 1 collects the money and 2 denote where person 2 does so, then a possible solution is 1 1 1 2 0 1 2 2 1
Input: 0 9 9 3 3 2 9 3 3 3 0 3 3 3 3 4 1 1 1 1 Output: 52
Approach: The problem can be solved by using the recursion, by moving both the persons down and right in each of the cell and finding the maximum path sum in all the paths from (0, 0) to (N-1, M-1). So the idea is to find the cost of all possible paths and to find the maximum of them.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const static int MAXR = 20, MAXC = 20;
int cache[MAXC][MAXR][MAXC][MAXR],
dp[MAXC][MAXR][MAXC][MAXR];
int n, m;
vector<string> grid;
int maxMoney( int x1, int y1, int x2, int y2)
{
if (x1 >= n || y1 >= m || x2 >= n || y2 >= m)
return 0;
if (cache[x1][y1][x2][y2] != 0)
return dp[x1][y1][x2][y2];
cache[x1][y1][x2][y2] = 1;
int money = grid[y1][x1] - '0' ;
if (x1 != x2 || y1 != y2)
money += grid[y2][x2] - '0' ;
return dp[x1][y1][x2][y2]
= money
+ max(
max(maxMoney(x1 + 1, y1, x2 + 1, y2),
maxMoney(x1, y1 + 1, x2 + 1, y2)),
max(maxMoney(x1 + 1, y1, x2, y2 + 1),
maxMoney(x1, y1 + 1, x2, y2 + 1)));
}
int32_t main()
{
n = 3;
m = 3;
grid.push_back( "111" );
grid.push_back( "101" );
grid.push_back( "111" );
cout << maxMoney(0, 0, 0, 0);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int MAXR = 20 , MAXC = 20 ;
static int [][][][]cache = new int [MAXC][MAXR][MAXC][MAXR];
static int [][][][]dp = new int [MAXC][MAXR][MAXC][MAXR];
static int n, m;
static Vector<String> grid = new Vector<String>();
static int maxMoney( int x1, int y1, int x2, int y2)
{
if (x1 >= n || y1 >= m || x2 >= n || y2 >= m)
return 0 ;
if (cache[x1][y1][x2][y2] != 0 )
return dp[x1][y1][x2][y2];
cache[x1][y1][x2][y2] = 1 ;
int money = grid.get(y1).charAt(x1)- '0' ;
if (x1 != x2 || y1 != y2)
money += grid.get(y2).charAt(x2) - '0' ;
return dp[x1][y1][x2][y2]
= money
+ Math.max(
Math.max(maxMoney(x1 + 1 , y1, x2 + 1 , y2),
maxMoney(x1, y1 + 1 , x2 + 1 , y2)),
Math.max(maxMoney(x1 + 1 , y1, x2, y2 + 1 ),
maxMoney(x1, y1 + 1 , x2, y2 + 1 )));
}
public static void main(String[] args)
{
n = 3 ;
m = 3 ;
grid.add( "111" );
grid.add( "101" );
grid.add( "111" );
System.out.print(maxMoney( 0 , 0 , 0 , 0 ));
}
}
|
Python3
MAXR, MAXC = 20 , 20
cache = [[[[ 0 for i in range (MAXR)] for j in range (MAXC)] for k in range (MAXR)] for l in range (MAXC)]
dp = [[[[ 0 for i in range (MAXR)] for j in range (MAXC)] for k in range (MAXR)] for l in range (MAXC)]
grid = []
def maxMoney(x1, y1, x2, y2):
if (x1 > = n or y1 > = m or x2 > = n or y2 > = m):
return 0
if (cache[x1][y1][x2][y2] ! = 0 ):
return dp[x1][y1][x2][y2]
cache[x1][y1][x2][y2] = 1
money = ord (grid[y1][x1]) - ord ( '0' )
if (x1 ! = x2 or y1 ! = y2):
money + = ord (grid[y2][x2]) - ord ( '0' )
dp[x1][y1][x2][y2] = money + max ( max (maxMoney(x1 + 1 , y1, x2 + 1 , y2), maxMoney(x1, y1 + 1 , x2 + 1 , y2)),
max (maxMoney(x1 + 1 , y1, x2, y2 + 1 ),
maxMoney(x1, y1 + 1 , x2, y2 + 1 )))
return dp[x1][y1][x2][y2]
n = 3
m = 3
grid.append( "111" )
grid.append( "101" )
grid.append( "111" )
print (maxMoney( 0 , 0 , 0 , 0 ))
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static int MAXR = 20, MAXC = 20;
static int [,,,]cache = new int [MAXC,MAXR,MAXC,MAXR];
static int [,,,]dp = new int [MAXC,MAXR,MAXC,MAXR];
static int n, m;
static List<String> grid = new List<String>();
static int maxMoney( int x1, int y1, int x2, int y2)
{
if (x1 >= n || y1 >= m || x2 >= n || y2 >= m)
return 0;
if (cache[x1,y1,x2,y2] != 0)
return dp[x1,y1,x2,y2];
cache[x1,y1,x2,y2] = 1;
int money = grid[y1][x1]- '0' ;
if (x1 != x2 || y1 != y2)
money += grid[y2][x2] - '0' ;
return dp[x1,y1,x2,y2]
= money
+ Math.Max(
Math.Max(maxMoney(x1 + 1, y1, x2 + 1, y2),
maxMoney(x1, y1 + 1, x2 + 1, y2)),
Math.Max(maxMoney(x1 + 1, y1, x2, y2 + 1),
maxMoney(x1, y1 + 1, x2, y2 + 1)));
}
public static void Main(String[] args)
{
n = 3;
m = 3;
grid.Add( "111" );
grid.Add( "101" );
grid.Add( "111" );
Console.Write(maxMoney(0, 0, 0, 0));
}
}
|
Javascript
<script>
let MAXR = 20, MAXC = 20;
let cache = new Array(MAXC);
let dp = new Array(MAXC);
for (let i = 0; i < MAXR; i++)
{
dp[i] = new Array(MAXR);
cache[i] = new Array(MAXR);
for (let j = 0; j < MAXC; j++)
{
dp[i][j] = new Array(MAXC);
cache[i][j] = new Array(MAXC);
for (let k = 0; k < MAXR; k++)
{
dp[i][j][k] = new Array(MAXR);
cache[i][j][k] = new Array(MAXR);
for (let l = 0; l < MAXR; l++)
{
dp[i][j][k][l] = 0;
cache[i][j][k][l] = 0;
}
}
}
}
let n, m;
let grid = [];
function maxMoney(x1, y1, x2, y2)
{
if (x1 >= n || y1 >= m || x2 >= n || y2 >= m)
return 0;
if (cache[x1][y1][x2][y2] != 0)
return dp[x1][y1][x2][y2];
cache[x1][y1][x2][y2] = 1;
let money = grid[y1][x1]- '0' ;
if (x1 != x2 || y1 != y2)
money += grid[y2][x2] - '0' ;
dp[x1][y1][x2][y2]
= money
+ Math.max(
Math.max(maxMoney(x1 + 1, y1, x2 + 1, y2),
maxMoney(x1, y1 + 1, x2 + 1, y2)),
Math.max(maxMoney(x1 + 1, y1, x2, y2 + 1),
maxMoney(x1, y1 + 1, x2, y2 + 1)));
return dp[x1][y1][x2][y2];
}
n = 3;
m = 3;
grid.push( "111" );
grid.push( "101" );
grid.push( "111" );
document.write(maxMoney(0, 0, 0, 0));
</script>
|
Time Complexity: O(2N*2M) Auxiliary Space: O((N*M)2)
|