简体   繁体   English

全为 1 的最大尺寸矩形二进制子矩阵

[英]Maximum size rectangle binary sub-matrix with all 1s

I wrote this code with the histogram solution but instead of entering the matrix on the code, the user will input its matrix.我用直方图解决方案编写了这段代码,但不是在代码上输入矩阵,而是用户将输入其矩阵。 now idk what I'm doing wrong, everything seems to work except the math of the histogram.现在知道我做错了什么,除了直方图的数学之外,一切似乎都有效。 What am I doing wrong?我究竟做错了什么?

The user will input the rows and columns, then one by one, each value in the matrix.用户将输入行和列,然后一一输入矩阵中的每个值。 then the code will show the matrix and calculate the Maximum size rectangle binary sub-matrix with all 1s.然后代码将显示矩阵并计算最大尺寸矩形二进制子矩阵,全为 1。

#include <stdio.h>
#include <bits/stdc++.h>
#include <iostream>

using namespace std;
 
#define MAXROW      100
#define MAXCOL      100
 
int maxHist(int row[]){
        stack<int> result; 
    int top_val;
    int max_area = 0;
    int area = 0; 
    int a = 0; 
    while (a < MAXCOL) 
   { 
       if (result.empty() || row[result.top()] <= row[a]) 
            result.push(a++); 
        else
        { 
            top_val = row[result.top()]; 
            result.pop(); 
            area = top_val * a; 
            if (!result.empty()) 
                area = top_val * (a - result.top() - 1 ); 
           max_area = max(area, max_area); 
        } 
    } 
    while (!result.empty()) 
    { 
      top_val = row[result.top()]; 
       result.pop(); 
       area = top_val * a; 
       if (!result.empty()) 
            area = top_val * (a - result.top() - 1 ); 
        max_area = max(area, max_area); 
    } 
    return max_area; 
}
int maxRectangle(int A[][MAXCOL])  {
        int result = maxHist(A[0]); 
    for (int a = 1; a < MAXROW; a++) 
    { 
        for (int b = 0; b < MAXCOL; b++) 
            if (A[a][b]) A[a][b] += A[a - 1][b]; 
        result = max(result, maxHist(A[a])); 
    } 
    return result; 
}
 
int main(){
    int matrix[MAXROW][MAXCOL];
    int i,j,x,y
    ;
    cout << "Numero de linhas na matrix: ";
    cin >> i;
    cout << "Numero de colunas na matrix: ";
    cin >> j;
    float l[i][j];
    int p = 0, q = 0;
    while (p < i) {
        while (q < j) {
          cout << "Entre o valor [" << p + 1 << "," << q + 1 << "] da matrix: ";
          cin >> l[p][q];
          q = q + 1;
        }
        p = p + 1;
        q = 0;
    }
    int A[][MAXCOL] = { l[p][q]}; 
    cout << "\nA matrix e:\n ";
    for(x=0;x< i;x++){
         for(y=0;y< j;y++){
            cout << l[x][y]<< " ";
        }
        cout << "\n";   
    }
    cout << "Area of maximum rectangle is "
    << maxRectangle(A); 
 return 0;   
}
        

This line:这一行:

int A[][MAXCOL] = { l[p][q]};

declares A and assigns l[p][q] to the first element, and zeroes everything else.声明 A 并将l[p][q]分配给第一个元素,并将其他所有元素归零。 You would need a loop to copy l to A .您需要一个循环将l复制到A Even better, use std::vector instead of static arrays with maximize sizes.更好的是,使用std::vector而不是具有最大化大小的静态数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM