using System.Collections.Generic;
public static void Main()
int []knightPos = {1, 1};
int []targetPos = {30, 30};
Console.WriteLine(MinDistance(knightPos, targetPos, N));
private static bool InBounds(int x, int y, int n)
return x >= 1 && x <= n && y >= 1 && y <= n;
private static int MinDistance(int[] knightPos, int[] target, int n)
int[] dx = new int[] { -2, -1, 1, 2, -2, -1, 1, 2 };
int[] dy = new int[] { -1, -2, -2, -1, 1, 2, 2, 1 };
bool[,] visited = new bool[n+1, n+1];
Queue<Cell> queue = new Queue<Cell>();
queue.Enqueue(new Cell(knightPos[0], knightPos[1], 0));
visited[knightPos[0], knightPos[1]] = true;
Cell currentCell = queue.Dequeue();
if(currentCell.x == target[0] && currentCell.y == target[1])
for(int i = 0; i < 8; i++)
x = currentCell.x + dx[i];
y = currentCell.y + dy[i];
if(InBounds(x, y, n) && !visited[x, y])
queue.Enqueue(new Cell(x, y, currentCell.dis + 1));
public int dis {get;set;}
public Cell(int x, int y, int dis)