Maximum Chocolate Pickup Using Two Robots (original) (raw)
You’re given a 2D array **grid[][] where grid[i][j] is the number of chocolates in each cell.
Two robots start at:
- Robot 1 -> top-left (0, 0)
- Robot 2 -> top-right (0, cols – 1)
In each step, both robots move to the next row - either (i+1, j-1), (i+1, j), or ****(i+1, j+1)**. They collect all chocolates from the cells they visit. If both land on the same cell, chocolates are counted only once. Robots must stay inside the grid and finish in the bottom row.
Find the **maximum chocolates both robots can collect.
**Examples:
**Input: grid[][]= [[4, 1, 2],
[3, 6, 1],
[1, 6, 6],
[3, 1, 2]]
**Output: 32
**Explanation:
The path followed by first robot is: (0, 0) -> (1, 0) -> (2, 1) -> (3, 0), so total chocolates collected: 4 + 3 + 6 + 3 = 16
The path followed by second robot is: (0, 2) -> (1, 1) -> (2, 2) -> (3, 2), so total chocolates connected: 2 + 6 + 6 + 2 = 16.
Total Chocolates collected by both the robots: 16 + 16 = 32
**Input: grid[][] = [[2, 1, 2, 1],
[1, 1, 1, 6],
[5, 0, 8, 0],
[4, 5, 4, 5]]
**Output: 33
**Explanation:
The path followed by robot is: (0, 0) -> (1, 0) -> (2, 0) -> (3, 1), so total chocolates collected: 2 + 1 + 5 + 5 = 13
The path followed by second robot is: (0, 3) -> (1, 3) -> (2, 2) -> (3, 3), so total chocolates collected: 1 + 6 + 8 + 5 = 20.
Total Chocolates collected by both the robots: 13 + 20 = 33
Table of Content
- [Naive Approach] - Using Recursion – O(9^n) Time and O(n) Space
- [Better Approach - 1] - Using Memoization – O(n*(m^2)) Time and O(n*(m^2)) Space
- [Better Approach - 2] - Using Tabulation – O(n*(m^2)) Time and O(n*(m^2)) Space
Both robots start at (0, 0) and (0, cols − 1). At each step, they move to the next row, choosing left-diagonal, right-diagonal, or straight down. If both land on the same cell, chocolates are counted once; otherwise, both are added.
[Naive Approach] - Using Recursion – O(9^n) Time and O(n) Space
In order to get the maximum chocolates from both robots, we explore all the possibilities and take the maximum among them. We can solve this using recursion by exploring all three choices for each robot and choosing the combination that gives the maximum chocolates among them.
C++ `
//Driver Code Starts #include #include #include using namespace std;
//Driver Code Ends
int findMaxChocolates(int row, int col1, int col2, vector<vector> &grid) { int n = grid.size(); int m = grid[0].size();
// return 0 as there are no more cells to move.
if (row == n) return 0;
// either of the robot is outside the grid
if (col1 < 0 || col2 < 0 || col1 >= m || col2 >= m) return INT_MIN;
// robots can move vertically in different directions
int dir[] = {-1, 0, 1};
int maxVal = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int d1 = dir[i];
int d2 = dir[j];
maxVal = max(maxVal, findMaxChocolates(row + 1, col1 + d1, col2 + d2, grid));
}
}
// robot 1 picking chocolates from current cell
maxVal += grid[row][col1];
// robot 2 picking chocolates from current cell
// if cell is not same
if (col1 != col2) maxVal += grid[row][col2];
return maxVal;}
int maxChocolate(vector<vector> &grid) { int m = grid[0].size(); return findMaxChocolates(0, 0, m - 1, grid); }
//Driver Code Starts int main() { vector<vector> grid = { {4, 1, 2}, {3, 6, 1}, {1, 6, 6}, {3, 1, 2} };
cout << maxChocolate(grid);} //Driver Code Ends
Java
//Driver Code Starts import java.util.ArrayList; import java.util.List;
class GFG { //Driver Code Ends
static int findMaxChocolates( int row, int col1, int col2, int[][] grid ) {
int n = grid.length;
int m = grid[0].length;
// return 0 as there are no more cells to move.
if( row == n ) return 0;
// either of the robot is outside the grid
if( col1 < 0 || col2 < 0 || col1 >= m || col2 >= m ) return Integer.MIN_VALUE;
// robots can move vertically in different directions
int[] dir = {-1, 0, 1};
int max = 0;
for( int i = 0; i < dir.length; i++ ) {
for( int j = 0; j < dir.length; j++ ) {
int d1 = dir[i];
int d2 = dir[j];
max = Math.max(max, findMaxChocolates(row+1, col1+d1, col2+d2, grid));
}
}
// robot 1 picking chocolates from current cell
max += grid[row][col1];
// robot 2 picking chocolates from current cell
// if cell is not same
if( col1 != col2 ) max += grid[row][col2];
return max;
}
static int maxChocolate(int[][] grid ) {
int m = grid[0].length;
return findMaxChocolates(0, 0, m-1, grid);
}//Driver Code Starts public static void main(String[] args) { int[][] grid = { {4, 1, 2}, {3, 6, 1}, {1, 6, 6}, {3, 1, 2} };
System.out.println(maxChocolate(grid));
}} //Driver Code Ends
Python
def findMaxChocolates(row, col1, col2, grid): n = len(grid) m = len(grid[0])
# return 0 as there are no more cells to move.
if row == n:
return 0
# either of the robot is outside the grid
if col1 < 0 or col2 < 0 or col1 >= m or col2 >= m:
return float('-inf')
# robots can move vertically in different directions
dir = [-1, 0, 1]
max_val = 0
for d1 in dir:
for d2 in dir:
max_val = max(max_val, findMaxChocolates(row + 1, col1 + d1, col2 + d2, grid))
# robot 1 picking chocolates from current cell
max_val += grid[row][col1]
# robot 2 picking chocolates from current cell
# if cell is not same
if col1 != col2:
max_val += grid[row][col2]
return max_valdef maxChocolate(grid): m = len(grid[0]) return findMaxChocolates(0, 0, m - 1, grid)
#Driver Code Starts if name == "main": grid = [ [4, 1, 2], [3, 6, 1], [1, 6, 6], [3, 1, 2] ]
print(maxChocolate(grid))#Driver Code Ends
C#
//Driver Code Starts using System; using System.Collections.Generic;
class GFG { //Driver Code Ends
static int findMaxChocolates(int row, int col1, int col2, int[][] grid) {
int n = grid.Length;
int m = grid[0].Length;
// return 0 as there are no more cells to move.
if (row == n) return 0;
// either of the robot is outside the grid
if (col1 < 0 || col2 < 0 || col1 >= m || col2 >= m) return int.MinValue;
// robots can move vertically in different directions
int[] dir = { -1, 0, 1 };
int max = 0;
for (int i = 0; i < dir.Length; i++) {
for (int j = 0; j < dir.Length; j++) {
int d1 = dir[i];
int d2 = dir[j];
max = Math.Max(max, findMaxChocolates(row + 1, col1 + d1, col2 + d2, grid));
}
}
// robot 1 picking chocolates from current cell
max += grid[row][col1];
// robot 2 picking chocolates from current cell
// if cell is not same
if (col1 != col2) max += grid[row][col2];
return max;
}
static int maxChocolate(int[][] grid) {
int m = grid[0].Length;
return findMaxChocolates(0, 0, m - 1, grid);
}//Driver Code Starts
static void Main() {
int[][] grid = {
new int[] {4, 1, 2},
new int[] {3, 6, 1},
new int[] {1, 6, 6},
new int[] {3, 1, 2}
};
Console.WriteLine(maxChocolate(grid));
}}
//Driver Code Ends
JavaScript
function findMaxChocolates(row, col1, col2, a) { const n = grid.length; const m = grid[0].length;
// return 0 as there are no more cells to move.
if (row === n) return 0;
// either of the robot is outside the grid
if (col1 < 0 || col2 < 0 || col1 >= m || col2 >= m) return -Infinity;
// robots can move vertically in different directions
const dir = [-1, 0, 1];
let max = 0;
for (let i = 0; i < dir.length; i++) {
for (let j = 0; j < dir.length; j++) {
const d1 = dir[i];
const d2 = dir[j];
max = Math.max(max, findMaxChocolates(row + 1, col1 + d1, col2 + d2, grid));
}
}
// robot 1 picking chocolates from current cell
max += grid[row][col1];
// robot 2 picking chocolates from current cell
// if cell is not same
if (col1 !== col2) max += grid[row][col2];
return max;}
function maxChocolate(grid) { const m = grid[0].length; return findMaxChocolates(0, 0, m - 1, grid); }
//Driver Code Starts // Driver code const grid = [ [4, 1, 2], [3, 6, 1], [1, 6, 6], [3, 1, 2] ];
console.log(maxChocolate(grid));
//Driver Code Ends
`
[Better Approach - 1] - Using Memoization – O(n*(m^2)) Time and O(n*(m^2)) Space
Many subproblems overlap because the same positions are encountered repeatedly during computation. To avoid recalculating these cases, their results are stored in a 3D table with states (i, j1, j2) representing the row i and the columns of the two robots, and reused whenever needed, which significantly improves efficiency.
C++ `
//Driver Code Starts #include #include #include using namespace std;
//Driver Code Ends
int findMaxChocolates(int row, int col1, int col2, vector<vector> &a, vector<vector<vector>> &dp) { int n = a.size(); int m = a[0].size();
// return 0 as there are no more cells to move.
if (row == n) return 0;
// either of the robot is outside the grid
if (col1 < 0 || col2 < 0 || col1 >= m || col2 >= m) return INT_MIN;
if (dp[row][col1][col2] != -1) return dp[row][col1][col2];
// robots can move vertically in different directions
int dir[] = {-1, 0, 1};
int maxVal = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int d1 = dir[i];
int d2 = dir[j];
maxVal = max(maxVal, findMaxChocolates(row + 1, col1 + d1, col2 + d2, a, dp));
}
}
// robot 1 picking chocolates from current cell
maxVal += a[row][col1];
// robot 2 picking chocolates from current cell
// if cell is not same
if (col1 != col2) maxVal += a[row][col2];
// storing the answer for current cell
return dp[row][col1][col2] = maxVal;}
int maxChocolate(vector<vector> &grid) { int n = grid.size(); int m = grid[0].size(); vector<vector<vector>> dp(n, vector<vector>(m, vector(m, -1)));
return findMaxChocolates(0, 0, m - 1, grid, dp);}
//Driver Code Starts
int main() { vector<vector> grid = { {4, 1, 2}, {3, 6, 1}, {1, 6, 6}, {3, 1, 2} }; cout << maxChocolate(grid); }
//Driver Code Ends
Java
//Driver Code Starts import java.util.ArrayList; import java.util.Arrays;
class GFG { //Driver Code Ends
static int findMaxChocolates(int row, int col1, int col2, int[][] grid, int[][][] dp) {
int n = grid.length;
int m = grid[0].length;
// return 0 as there are no more cells to move.
if( row == n ) return 0;
// either of the robot is outside the grid
if( col1 < 0 || col2 < 0 || col1 >= m || col2 >= m ) return Integer.MIN_VALUE;
if( dp[row][col1][col2] != -1 ) return dp[row][col1][col2];
// robots can move vertically in different directions
int[] dir = {-1, 0, 1};
int max = 0;
for( int i = 0; i < dir.length; i++ ) {
for( int j = 0; j < dir.length; j++ ) {
int d1 = dir[i];
int d2 = dir[j];
max = Math.max(max, findMaxChocolates(row+1, col1+d1, col2+d2, grid, dp));
}
}
// robot 1 picking chocolates from current cell
max += grid[row][col1];
// robot 2 picking chocolates from current cell
// if cell is not same
if( col1 != col2 ) max += grid[row][col2];
// storing the answer for current cell
return dp[row][col1][col2] = max;
}
static int maxChocolate(int[][] grid ) {
int n = grid.length;
int m = grid[0].length;
int[][][] dp = new int[n][m][m];
for( int[][] rows : dp ) {
for( int[] row : rows ) Arrays.fill(row, -1);
}
return findMaxChocolates(0, 0, m-1, grid, dp);
}//Driver Code Starts
public static void main(String[] args) {
int[][] grid = {
{4, 1, 2},
{3, 6, 1},
{1, 6, 6},
{3, 1, 2}
};
System.out.println(maxChocolate(grid));
}} //Driver Code Ends
Python
def findMaxChocolates(row, col1, col2, grid, dp): n = len(grid) m = len(grid[0])
# return 0 as there are no more cells to move.
if row == n:
return 0
# either of the robot is outside the grid
if col1 < 0 or col2 < 0 or col1 >= m or col2 >= m:
return float('-inf')
if dp[row][col1][col2] != -1:
return dp[row][col1][col2]
# robots can move vertically in different directions
dir = [-1, 0, 1]
max_val = 0
for d1 in dir:
for d2 in dir:
max_val = max(max_val, findMaxChocolates(row + 1, col1 + d1, col2 + d2, grid, dp))
# robot 1 picking chocolates from current cell
max_val += grid[row][col1]
# robot 2 picking chocolates from current cell
# if cell is not same
if col1 != col2:
max_val += grid[row][col2]
# storing the answer for current cell
dp[row][col1][col2] = max_val
return max_valdef maxChocolate(grid): n = len(grid) m = len(grid[0]) dp = [[[-1 for _ in range(m)] for _ in range(m)] for _ in range(n)]
return findMaxChocolates(0, 0, m - 1, grid, dp)#Driver Code Starts if name == "main": grid = [ [4, 1, 2], [3, 6, 1], [1, 6, 6], [3, 1, 2] ]
print(maxChocolate(grid))#Driver Code Ends
C#
//Driver Code Starts using System; using System.Collections.Generic;
class GFG { //Driver Code Ends
static int findMaxChocolates(int row, int col1, int col2, int[][] grid, int[][][] dp) {
int n = grid.Length;
int m = grid[0].Length;
// return 0 as there are no more cells to move.
if (row == n) return 0;
// either of the robot is outside the grid
if (col1 < 0 || col2 < 0 || col1 >= m || col2 >= m) return int.MinValue;
if (dp[row][col1][col2] != -1) return dp[row][col1][col2];
// robots can move vertically in different directions
int[] dir = { -1, 0, 1 };
int max = 0;
for (int i = 0; i < dir.Length; i++) {
for (int j = 0; j < dir.Length; j++) {
int d1 = dir[i];
int d2 = dir[j];
max = Math.Max(max, findMaxChocolates(row + 1, col1 + d1, col2 + d2, grid, dp));
}
}
// robot 1 picking chocolates from current cell
max += grid[row][col1];
// robot 2 picking chocolates from current cell
// if cell is not same
if (col1 != col2) max += grid[row][col2];
// storing the answer for current cell
return dp[row][col1][col2] = max;
}
static int maxChocolate(int[][] grid) {
int n = grid.Length;
int m = grid[0].Length;
int[][][] dp = new int[n][][];
for (int i = 0; i < n; i++) {
dp[i] = new int[m][];
for (int j = 0; j < m; j++) {
dp[i][j] = new int[m];
Array.Fill(dp[i][j], -1);
}
}
return findMaxChocolates(0, 0, m - 1, grid, dp);
}//Driver Code Starts
static void Main() {
int[][] grid = {
new int[] {4, 1, 2},
new int[] {3, 6, 1},
new int[] {1, 6, 6},
new int[] {3, 1, 2}
};
Console.WriteLine(maxChocolate(grid));
}}
//Driver Code Ends
JavaScript
function findMaxChocolates(row, col1, col2, grid, dp) { const n = grid.length; const m = grid[0].length;
// return 0 as there are no more cells to move.
if (row === n) return 0;
// either of the robot is outside the grid
if (col1 < 0 || col2 < 0 || col1 >= m || col2 >= m) return -Infinity;
if (dp[row][col1][col2] !== -1) return dp[row][col1][col2];
// robots can move vertically in different directions
const dir = [-1, 0, 1];
let max = 0;
for (let i = 0; i < dir.length; i++) {
for (let j = 0; j < dir.length; j++) {
const d1 = dir[i];
const d2 = dir[j];
max = Math.max(max, findMaxChocolates(row + 1, col1 + d1, col2 + d2, grid, dp));
}
}
// robot 1 picking chocolates from current cell
max += grid[row][col1];
// robot 2 picking chocolates from current cell
// if cell is not same
if (col1 !== col2) max += grid[row][col2];
// storing the answer for current cell
return dp[row][col1][col2] = max;}
function maxChocolate(grid) { const n = grid.length; const m = grid[0].length; const dp = Array.from({ length: n }, () => Array.from({ length: m }, () => Array(m).fill(-1)) );
return findMaxChocolates(0, 0, m - 1, grid, dp);}
const grid = [ //Driver Code Starts [4, 1, 2], [3, 6, 1], [1, 6, 6], [3, 1, 2] ];
console.log(maxChocolate(grid));
//Driver Code Ends
`
[Better Approach - 2] - Using Tabulation – O(n*(m^2)) Time and O(n*(m^2)) Space
In this approach, we iteratively calculate the answers starting from the smallest subproblems - the last row of the grid. The answer for any cell where the robots are at positions (i, j1, j2) depends on the results of the next row for all possible moves of both robots. Using the precomputed results from the next row, we can efficiently compute the maximum chocolates for the current row and store them for reuse, continuing this process upward until we reach the top.
C++ `
//Driver Code Starts #include #include #include using namespace std;
//Driver Code Ends
int maxFromNextRow(int r, int c1, int c2, int m, vector<vector<vector>> &dp) { int dir[] = {-1, 0, 1}; int maxVal = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { int d1 = dir[i]; int d2 = dir[j];
// checking if next cell doesn't lie outside the grid
if (c1 + d1 >= 0 && c1 + d1 < m && c2 + d2 >= 0 && c2 + d2 < m) {
maxVal = max(maxVal, dp[r + 1][c1 + d1][c2 + d2]);
}
}
}
return maxVal;}
int maxChocolate(vector<vector> &grid) { int n = grid.size(); int m = grid[0].size(); vector<vector<vector>> dp(n, vector<vector>(m, vector(m, 0)));
// the maximum chocolates to be picked
// if the robot starts in the last row
// is the number of chocolates in the cell
for (int c1 = 0; c1 < m; c1++) {
for (int c2 = 0; c2 < m; c2++) {
dp[n - 1][c1][c2] = (grid[n - 1][c1]) + ((c1 != c2) ? grid[n - 1][c2] : 0);
}
}
for (int r = n - 2; r >= 0; r--) {
for (int c1 = 0; c1 < m; c1++) {
for (int c2 = 0; c2 < m; c2++) {
dp[r][c1][c2] = (grid[r][c1]) + ((c1 != c2) ? grid[r][c2] : 0);
// getting the max number of chocolates
// that can be picked from next row
int maxVal = maxFromNextRow(r, c1, c2, m, dp);
dp[r][c1][c2] += maxVal;
}
}
}
return dp[0][0][m - 1];}
//Driver Code Starts int main() { vector<vector> grid = { {4, 1, 2}, {3, 6, 1}, {1, 6, 6}, {3, 1, 2} }; cout << maxChocolate(grid); } //Driver Code Ends
Java
//Driver Code Starts import java.util.ArrayList; import java.util.Arrays;
class GFG { //Driver Code Ends
static int maxFromNextRow( int r, int c1, int c2, int m, int[][][] dp) {
int[] dir = {-1, 0, 1};
int max = 0;
for( int i = 0; i < dir.length; i++ ) {
for( int j = 0; j < dir.length; j++ ) {
int d1 = dir[i];
int d2 = dir[j];
// checking if next cell doesn't lie outside the grid
if( c1 + d1 >= 0 && c1 + d1 < m && c2 + d2 >= 0 && c2 + d2 < m ) {
max = Math.max(max, dp[r+1][c1+d1][c2+d2]);
}
}
}
return max;
}
static int maxChocolate(int[][] grid ) {
int n = grid.length;
int m = grid[0].length;
int[][][] dp = new int[n][m][m];
// the maximum chocolates to be picked
// if the robot starts in the last row
// is the number of chocolates in the cell
for( int c1 = 0; c1 < m ; c1++ ) {
for( int c2 = 0; c2 < m ; c2++ ) {
dp[n-1][c1][c2] = (grid[n-1][c1]) + ((c1 != c2) ? grid[n-1][c2] : 0) ;
}
}
for( int r = n-2; r >= 0; r-- ) {
for( int c1 = 0; c1 < m ; c1++ ) {
for( int c2 = 0; c2 < m ; c2++ ) {
dp[r][c1][c2] = (grid[r][c1]) + ((c1 != c2) ? grid[r][c2] : 0) ;
// getting the max number of chocolates
// that can be picked from next row
int max = maxFromNextRow(r, c1, c2, m, dp);
dp[r][c1][c2] += max;
}
}
}
return dp[0][0][m-1];
}//Driver Code Starts
public static void main(String[] args) {
int[][] grid = {
{4, 1, 2},
{3, 6, 1},
{1, 6, 6},
{3, 1, 2}
};
System.out.println(maxChocolate(grid));
}} //Driver Code Ends
Python
def maxFromNextRow(r, c1, c2, m, dp): dir = [-1, 0, 1] max_val = 0 for d1 in dir: for d2 in dir: # checking if next cell doesn't lie outside the grid if 0 <= c1 + d1 < m and 0 <= c2 + d2 < m: max_val = max(max_val, dp[r + 1][c1 + d1][c2 + d2]) return max_val
def maxChocolate(a): n = len(grid) m = len(grid[0]) dp = [[[0 for _ in range(m)] for _ in range(m)] for _ in range(n)]
# the maximum chocolates to be picked
# if the robot starts in the last row
# is the number of chocolates in the cell
for c1 in range(m):
for c2 in range(m):
dp[n - 1][c1][c2] = grid[n - 1][c1] + (grid[n - 1][c2] if c1 != c2 else 0)
for r in range(n - 2, -1, -1):
for c1 in range(m):
for c2 in range(m):
dp[r][c1][c2] = grid[r][c1] + (grid[r][c2] if c1 != c2 else 0)
# getting the max number of chocolates
# that can be picked from next row
max_val = maxFromNextRow(r, c1, c2, m, dp)
dp[r][c1][c2] += max_val
return dp[0][0][m - 1]if name == "main": #Driver Code Starts grid = [ [4, 1, 2], [3, 6, 1], [1, 6, 6], [3, 1, 2] ]
print(maxChocolate(grid))#Driver Code Ends
C#
//Driver Code Starts using System;
class GFG { //Driver Code Ends
static int maxFromNextRow(int r, int c1, int c2, int m, int[][][] dp) {
int[] dir = { -1, 0, 1 };
int max = 0;
for (int i = 0; i < dir.Length; i++) {
for (int j = 0; j < dir.Length; j++) {
int d1 = dir[i];
int d2 = dir[j];
// checking if next cell doesn't lie outside the grid
if (c1 + d1 >= 0 && c1 + d1 < m && c2 + d2 >= 0 && c2 + d2 < m) {
max = Math.Max(max, dp[r + 1][c1 + d1][c2 + d2]);
}
}
}
return max;
}
static int maxChocolate(int[][] grid) {
int n = grid.Length;
int m = grid[0].Length;
int[][][] dp = new int[n][][];
for (int i = 0; i < n; i++) {
dp[i] = new int[m][];
for (int j = 0; j < m; j++) {
dp[i][j] = new int[m];
}
}
// the maximum chocolates to be picked
// if the robot starts in the last row
// is the number of chocolates in the cell
for (int c1 = 0; c1 < m; c1++) {
for (int c2 = 0; c2 < m; c2++) {
dp[n - 1][c1][c2] = (grid[n - 1][c1]) + ((c1 != c2) ? grid[n - 1][c2] : 0);
}
}
for (int r = n - 2; r >= 0; r--) {
for (int c1 = 0; c1 < m; c1++) {
for (int c2 = 0; c2 < m; c2++) {
dp[r][c1][c2] = (grid[r][c1]) + ((c1 != c2) ? grid[r][c2] : 0);
// getting the max number of chocolates
// that can be picked from next row
int max = maxFromNextRow(r, c1, c2, m, dp);
dp[r][c1][c2] += max;
}
}
}
return dp[0][0][m - 1];
}//Driver Code Starts
static void Main() {
int[][] grid = {
new int[] {4, 1, 2},
new int[] {3, 6, 1},
new int[] {1, 6, 6},
new int[] {3, 1, 2}
};
Console.WriteLine(maxChocolate(grid));
}} //Driver Code Ends
JavaScript
function maxFromNextRow(r, c1, c2, m, dp) { const dir = [ -1, 0, 1 ]; let max = 0; for (let i = 0; i < dir.length; i++) { for (let j = 0; j < dir.length; j++) { const d1 = dir[i]; const d2 = dir[j];
// checking if next cell doesn't lie outside the
// grid
if (c1 + d1 >= 0 && c1 + d1 < m && c2 + d2 >= 0
&& c2 + d2 < m) {
max = Math.max(max,
dp[r + 1][c1 + d1][c2 + d2]);
}
}
}
return max;}
function maxChocolate(a) { const n = grid.length; const m = a[0].length; const dp = Array.from( {length : n}, () => Array.from({length : m}, () => Array(m).fill(0)));
// the maximum chocolates to be picked
// if the robot starts in the last row
// is the number of chocolates in the cell
for (let c1 = 0; c1 < m; c1++) {
for (let c2 = 0; c2 < m; c2++) {
dp[n - 1][c1][c2]
= (grid[n - 1][c1])
+ ((c1 !== c2) ? grid[n - 1][c2] : 0);
}
}
for (let r = n - 2; r >= 0; r--) {
for (let c1 = 0; c1 < m; c1++) {
for (let c2 = 0; c2 < m; c2++) {
dp[r][c1][c2]
= (grid[r][c1])
+ ((c1 !== c2) ? grid[r][c2] : 0);
// getting the max number of chocolates
// that can be picked from next row
const max
= maxFromNextRow(r, c1, c2, m, dp);
dp[r][c1][c2] += max;
}
}
}
return dp[0][0][m - 1];}
//Driver Code Starts
// Driver code const grid = [ [ 4, 1, 2 ], [ 3, 6, 1 ], [ 1, 6, 6 ], [ 3, 1, 2 ] ];
console.log(maxChocolate(grid)); //Driver Code Ends
`



