using
System;
using
System.Collections.Generic;
public
class
GFG {
static
int
KnightInGeekland(
int
start_x,
int
start_y,
List<List<
int
> > arr)
{
int
n = arr.Count;
int
m = arr[0].Count;
List<List<
int
> > vis =
new
List<List<
int
> >(n);
for
(
int
i = 0; i < n; i++) {
vis.Add(
new
List<
int
>(m));
for
(
int
j = 0; j < m; j++) {
vis[i].Add(
0);
}
}
vis[start_x][start_y]
= 1;
Queue<Tuple<
int
,
int
> > q =
new
Queue<Tuple<
int
,
int
> >();
q.Enqueue(
new
Tuple<
int
,
int
>(
start_x,
start_y));
List<
int
> list
=
new
List<
int
>();
int
points = 0;
int
[] dx = {
-2, -1, 1, 2, 2, 1, -1, -2
};
int
[] dy = {
1, 2, 2, 1, -1, -2, -2, -1
};
Func<
int
,
int
,
bool
> isSafe = (i, j) => i >= 0 && j >= 0 && i < n && j < m;
while
(q.Count > 0)
{
int
size = q.Count;
points = 0;
for
(
int
i = 0; i < size;
i++)
{
Tuple<
int
,
int
> tmp
= q.Dequeue();
int
x = tmp.Item1,
y = tmp.Item2;
points += arr[x][y];
for
(
int
k = 0; k < 8; k++) {
int
xi = x + dx[k];
int
xj = y + dy[k];
if
(isSafe(xi, xj)
&& vis[xi][xj] == 0) {
vis[xi][xj]
= 1;
q.Enqueue(
new
Tuple<
int
,
int
>(
xi, xj));
}
}
}
list.Add(
points);
}
int
max = -1,
ans = -1;
for
(
int
i = list.Count - 1; i >= 0; i--) {
if
(list[i] + i < list.Count)
list[i] += list[i + list[i]];
}
for
(
int
i = 0; i < list.Count; i++) {
if
(list[i] > max) {
max = list[i];
ans = i;
}
}
return
ans;
}
static
void
Main()
{
int
start_x = 4, start_y = 5;
List<List<
int
> > arr =
new
List<List<
int
> >{
new
List<
int
>{ 0, 0, 0, 2, 0, 2, 0, 2, 0, 0 },
new
List<
int
>{ 0, 0, 2, 0, 2, 0, 2, 0, 2, 0 },
new
List<
int
>{ 0, 2, 0, 0, 1, 2, 0, 0, 0, 2 },
new
List<
int
>{ 0, 0, 2, 0, 2, 0, 2, 0, 2, 0 },
new
List<
int
>{ 0, 2, 0, 2, 0, 0, 0, 2, 0, 2 },
new
List<
int
>{ 0, 0, 2, 0, 2, 0, 2, 0, 2, 0 },
new
List<
int
>{ 0, 2, 0, 0, 0, 2, 0, 0, 0, 2 },
new
List<
int
>{ 0, 0, 2, 0, 2, 0, 2, 0, 2, 0 },
new
List<
int
>{ 0, 0, 0, 2, 0, 2, 0, 2, 0, 0 }
};
int
result
= KnightInGeekland(start_x, start_y, arr);
Console.WriteLine(
"The maximum score starting from the given position is: "
+ result);
}
}