# Steps by Knight

Given a square chessboard, the initial position of Knight and position of a target. Find out the minimum steps a Knight will take to reach the target position.

**Note:** index are zero-based.

```csharp
class Solution
{
    // There is only 8 possible way that a Knight can move within the chessboard. 
    private (int, int)[] directions = [(-1, -2), (1, -2), (2, -1), (2, 1), (1, 2), (-1, 2), (-2, 1), (-2, -1)];
    // Function to find out minimum steps Knight needs to reach target position.
    public int MinStepToReachTarget(List<int> KnightPos, List<int> TargetPos, int N)
    {
        bool[,] visited = new bool[N, N];
        Queue<(int, int, int)> queue = new Queue<(int, int, int)>();
        queue.Enqueue((KnightPos[0], KnightPos[1], 0));

        while (queue.Count > 0)
        {
            var (x, y, dis) = queue.Dequeue();
            if (x == TargetPos[0] && y == TargetPos[1])
            {
                return dis;
            }

            foreach ((int xP, int yP) in directions)
            {
                // move next step
                int newX = x + xP;
                int newY = y + yP;

                if (newX >= 0 && newX <= N && newY >= 0 && newY <= N && !visited[newX, newY])
                {
                    visited[newX, newY] = true;
                    queue.Enqueue((newX, newY, dis + 1));
                }
            }
        }

        return -1;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-57.gitbook.io/data-structure-and-algorithms/problems/graph/steps-by-knight.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
