Possible moves of knight

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)];

    public int FindPossibleMoves(int[,] mat, int p, int q)
    {
        int n = mat.Length;
        int count = 0;


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

            if (newX >= 0 && newX <= n && newY >= 0 && newY <= n && mat[newX , newY ] == 0)
            {
                count++;
            }
        }
        return count;
    }
}

Last updated