Esempio n. 1
0
        private static void MaxSubmatrixTest(int[,] matrix, int n)
        {
            int expected = MaxSubmatrixTestClass.GetMaxSubmatrixSum(matrix, n);
            int actual   = MaxSubmatrixClass.MaxSubmatrix(matrix);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 2
0
        public static int MaxSubmatrix(int[,] matrix)
        {
            int n = matrix.GetLength(0);

            if (matrix.GetLength(1) != n)
            {
                throw new ArgumentException(nameof(matrix));
            }

            int maxSum = int.MinValue;

            for (int i = 0; i < n; i++)
            {
                int[] sums = new int[n];

                for (int j = i; j < n; j++)
                {
                    for (int k = 0; k < n; k++)
                    {
                        sums[k] += matrix[j, k];
                    }

                    maxSum = Math.Max(maxSum, MaxSubmatrixClass.MaxSubArray(sums));
                }
            }

            return(maxSum);
        }