namespace Lab6
{
    public class Alg1
    {
        public int Begin(int[,] matrix)
        {
            return CalculateDeterminant(matrix);        
        }

        static int CalculateDeterminant(int[,] matrix)
        {
            int n = matrix.GetLength(0);

            if (n == 1)
            {
                return matrix[0, 0];
            }

            int determinant = 0;

            for (int j = 0; j < n; j++)
            {
                int[,] submatrix = new int[n - 1, n - 1];

                for (int i = 1; i < n; i++)
                {
                    for (int k = 0; k < n; k++)
                    {
                        if (k < j)
                        {
                            submatrix[i - 1, k] = matrix[i, k];
                        }
                        else if (k > j)
                        {
                            submatrix[i - 1, k - 1] = matrix[i, k];
                        }
                    }
                }

                determinant += (int)Math.Pow(-1, j) * matrix[0, j] * CalculateDeterminant(submatrix);
            }

            return determinant;
        }
    }
}
