public void GetMaxSumRectangle_should_return_Null_If_source_Matrix_Is_Empty()
        {
            int[,] source = new int[, ]
            {
                { }
            };

            var result = MatrixOperations.FindMaxSumRectangle(source);

            Assert.IsNull(result);
        }
示例#2
0
        private static void DemoFindMaxSumAnyRectangleInAMatrix()
        {
            //int[,] source = new int[,] {
            //        {10, 20, 30, 40},
            //        {40, 50, 60, 70},
            //        {80, 90, 100, 110},
            //        {120, 130, 140, 150}
            //    };

            int[,] source = new int[, ]
            {
                { 10, -1, -10 },
                { 0, 10, 0 },
                { 1, 20, 1 },
                { 1, 21, 1 }
            };

            Print(source, "Original matrix");
            Console.ForegroundColor = ConsoleColor.Green;

            var result = MatrixOperations.FindMaxSumRectangle(source);

            Console.WriteLine($"Max sum of rectangles: {result.Sum}.");

            var rows = source.GetLength(0);
            var cols = source.GetLength(1);

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    if (i >= result.RowStartIndex &&
                        i <= result.RowEndIndex &&
                        j >= result.ColumnStartIndex &&
                        j <= result.ColumnEndIndex)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }

                    Console.Write($"{source[i, j]} ");
                }
                Console.WriteLine();
            }
            Console.ForegroundColor = ConsoleColor.White;
        }
        public void GetMaxSumRectangle_should_return_max_sum_rectangle_of_1X3()
        {
            int[,] source = new int[, ]
            {
                { 10, -1, -10 }
            };

            var result = MatrixOperations.FindMaxSumRectangle(source);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Sum == 10);
            Assert.IsTrue(result.RowStartIndex == 0);
            Assert.IsTrue(result.RowEndIndex == 0);
            Assert.IsTrue(result.ColumnStartIndex == 0);
            Assert.IsTrue(result.ColumnEndIndex == 0);
        }