Untitled

 avatar
user_9831585
plain_text
a year ago
2.0 kB
0
Indexable
Never
#include <iostream>
void findMaxSubMatrix(int matrix[][5], int rows, int columns, int &startRow, int &startColumn, int &subRows, int &subColumns, int &sum);

using namespace std;
int main()
{
    int matrix[5][5];
    int rows, columns;
    cout << "row and col" << endl;
    cin >> rows;
    cin >> columns;

    cout << "Enter elements of the matrix: " << endl;
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            cin >> matrix[i][j];
        }
    }

    int startRow, startColumn, subRows, subColumns, sum;
    findMaxSubMatrix(matrix, rows, columns, startRow, startColumn, subRows, subColumns, sum);

    cout << "Maximum sub-matrix with only zeros starts at (" << startRow << ", " << startColumn << ") and has " << subRows << " rows and " << subColumns << " columns and has a sum of " << sum << "." << endl;

    return 0;
}

void findMaxSubMatrix(int matrix[][5], int rows, int columns, int &startRow, int &startColumn, int &subRows, int &subColumns, int &sum)
{
    int maxSum = 0;

    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            for (int k = i; k < rows; k++)
            {
                for (int l = j; l < columns; l++)
                {
                    int currentSum = 0;

                    for (int m = i; m <= k; m++)
                    {
                        for (int n = j; n <= l; n++)
                        {
                            currentSum += matrix[m][n];
                        }
                    }

                    if (currentSum == 0 && (k - i + 1) * (l - j + 1) > maxSum)
                    {
                        maxSum = (k - i + 1) * (l - j + 1);
                        startRow = i;
                        startColumn = j;
                        subRows = k - i + 1;
                        subColumns = l - j + 1;
                        sum = currentSum;
                    }
                }
            }
        }
    }
}