public void GetMaxSumRectangle_should_return_Null_If_Binary_Matrix_Is_Empty() { int[,] source = new int[, ] { { } }; var result = MatrixOperations.FindMaxSumRectangleWithContiguousElementsExcept(source, 0); Assert.IsNull(result); }
private static void DemoFindMaxSumContiguousRectangleInBinaryMatrix() { //int[,] source = new int[,] { // {10, 20, 30, 40}, // {40, 50, 60, 70}, // {80, 90, 100, 110}, // {120, 130, 140, 150} // }; int[,] source = new int[, ] { { 0, 1, 1 }, { 0, 1, 0 }, { 1, 1, 1 }, { 1, 1, 1 } }; Print(source, "Original matrix"); Console.ForegroundColor = ConsoleColor.Green; var result = MatrixOperations.FindMaxSumRectangleWithContiguousElementsExcept(source, 0); 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_In_Binary_Matrix_of_1X3() { int[,] source = new int[, ] { { 1, 0, 1 } }; var result = MatrixOperations.FindMaxSumRectangleWithContiguousElementsExcept(source, 0); Assert.IsNotNull(result); Assert.IsTrue(result.Sum == 1); Assert.IsTrue(result.RowStartIndex == 0); Assert.IsTrue(result.RowEndIndex == 0); Assert.IsTrue(result.ColumnStartIndex == 0); Assert.IsTrue(result.ColumnEndIndex == 0); }