using
System;
using
System.Collections.Generic;
class
Solution
{
public
static
int
KnightTravel(
int
[][] arr,
int
start_x,
int
start_y)
{
int
n = arr.Length;
int
m = arr[0].Length;
bool
[,] visited =
new
bool
[n, m];
visited[start_x, start_y] =
true
;
Queue<
int
[]> q =
new
Queue<
int
[]>();
q.Enqueue(
new
int
[] { start_x, start_y });
List<
int
> list =
new
List<
int
>();
int
points = 0;
while
(q.Count > 0)
{
int
size = q.Count;
points = 0;
for
(
int
i = 0; i < size; i++)
{
int
[] temp = q.Dequeue();
int
x = temp[0], y = temp[1];
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, n, m) && !visited[xi, xj])
{
visited[xi, xj] =
true
;
q.Enqueue(
new
int
[] { xi, xj });
}
}
}
list.Add(points);
}
int
max = -1, ans = -1;
for
(
int
i = list.Count - 1; i > -1; 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
bool
IsSafe(
int
i,
int
j,
int
n,
int
m)
{
return
i > -1 && i < n && j > -1 && j < m;
}
static
int
[] dx = { -2, -1, 1, 2, 2, 1, -1, -2 };
static
int
[] dy = { 1, 2, 2, 1, -1, -2, -2, -1 };
public
static
void
Main(
string
[] args)
{
int
[][] arr = {
new
int
[] { 0, 0, 0, 2, 0, 2, 0, 2, 0, 0 },
new
int
[] { 0, 0, 2, 0, 2, 0, 2, 0, 2, 0 },
new
int
[] { 0, 2, 0, 0, 1, 2, 0, 0, 0, 2 },
new
int
[] { 0, 0, 2, 0, 2, 0, 2, 0, 2, 0 },
new
int
[] { 0, 2, 0, 2, 0, 0, 0, 2, 0, 2 },
new
int
[] { 0, 0, 2, 0, 2, 0, 2, 0, 2, 0 },
new
int
[] { 0, 2, 0, 0, 0, 2, 0, 0, 0, 2 },
new
int
[] { 0, 0, 2, 0, 2, 0, 2, 0, 2, 0 },
new
int
[] { 0, 0, 0, 2, 0, 2, 0, 2, 0, 0 }
};
int
start_x = 4;
int
start_y = 5;
int
result = KnightTravel(arr, start_x, start_y);
Console.WriteLine(result);
}
}