using
System;
using
System.Collections.Generic;
public
class
GFG {
static
int
CalculateMaxChocolates(
int
currentRow,
int
robot1Col,
int
robot2Col,
int
totalRows,
int
totalCols,
List<List<
int
> > chocolateGrid,
List<List<List<
int
> > > dp)
{
if
(robot1Col < 0 || robot1Col >= totalCols
|| robot2Col < 0 || robot2Col >= totalCols)
return
int
.MinValue;
if
(currentRow == totalRows - 1) {
if
(robot1Col == robot2Col)
return
chocolateGrid[currentRow][robot1Col];
else
return
chocolateGrid[currentRow][robot1Col]
+ chocolateGrid[currentRow][robot2Col];
}
if
(dp[currentRow][robot1Col][robot2Col] != -1)
return
dp[currentRow][robot1Col][robot2Col];
int
maxChocolates =
int
.MinValue;
for
(
int
dRobot1 = -1; dRobot1 <= 1; dRobot1++) {
for
(
int
dRobot2 = -1; dRobot2 <= 1;
dRobot2++) {
int
chocolates = 0;
if
(robot1Col == robot2Col)
chocolates = chocolateGrid[currentRow]
[robot1Col];
else
chocolates = chocolateGrid[currentRow]
[robot1Col]
+ chocolateGrid[currentRow]
[robot2Col];
chocolates += CalculateMaxChocolates(
currentRow + 1, robot1Col + dRobot1,
robot2Col + dRobot2, totalRows,
totalCols, chocolateGrid, dp);
maxChocolates
= Math.Max(maxChocolates, chocolates);
}
}
return
dp[currentRow][robot1Col][robot2Col]
= maxChocolates;
}
static
void
Main()
{
int
totalRows = 4, totalCols = 3;
List<List<
int
> > chocolateGrid
=
new
List<List<
int
> >{
new
List<
int
>{ 4, 1, 2 },
new
List<
int
>{ 3, 6, 1 },
new
List<
int
>{ 1, 6, 6 },
new
List<
int
>{ 3, 1, 2 }
};
List<List<List<
int
> > > dp
=
new
List<List<List<
int
> > >();
for
(
int
i = 0; i < totalRows; i++) {
dp.Add(
new
List<List<
int
> >());
for
(
int
j = 0; j < totalCols; j++) {
dp[i].Add(
new
List<
int
>());
for
(
int
k = 0; k < totalCols; k++) {
dp[i][j].Add(-1);
}
}
}
int
result = CalculateMaxChocolates(
0, 0, totalCols - 1, totalRows, totalCols,
chocolateGrid, dp);
Console.WriteLine(
"The maximum number of chocolates that can be collected is: "
+ result);
}
}