using
System;
using
System.Collections.Generic;
class
GFG
{
static
int
ROW = 3;
static
int
COL = 3;
public
class
Point
{
public
int
x;
public
int
y;
public
Point(
int
x,
int
y)
{
this
.x = x;
this
.y = y;
}
}
static
Boolean isValid(Point p)
{
return
(p.x >= 0) && (p.y < COL);
}
static
int
find_max_cost(
int
[,]mat)
{
int
[,]max_val =
new
int
[ROW,COL];
max_val[ROW - 1, 0] = mat[ROW - 1, 0];
Point src =
new
Point(ROW - 1, 0);
Queue<Point> q =
new
Queue<Point>();
q.Enqueue(src);
while
(q.Count != 0)
{
Point curr = q.Peek();
q.Dequeue();
Point up =
new
Point(curr.x - 1, curr.y);
if
(isValid(up))
{
max_val[up.x, up.y] = Math.Max(max_val[up.x, up.y],
mat[up.x, up.y] + max_val[curr.x, curr.y]);
q.Enqueue(up);
}
Point right =
new
Point(curr.x,
curr.y + 1);
if
(isValid(right))
{
max_val[right.x, right.y] = Math.Max(max_val[right.x, right.y],
mat[right.x, right.y] + max_val[curr.x, curr.y]);
q.Enqueue(right);
}
}
return
max_val[0, COL - 1];
}
public
static
void
Main(String[] args)
{
int
[,]mat = {{20, -10, 0},
{1, 5, 10},
{1, 2, 3}};
Console.WriteLine(
"Given matrix is "
);
for
(
int
i = 0 ; i < ROW; ++i)
{
for
(
int
j = 0; j < COL; ++j)
Console.Write(mat[i, j] +
" "
);
Console.WriteLine();
}
Console.Write(
"Maximum cost is "
+
find_max_cost(mat));
}
}