This question is solved by brute force + dynamic planning. The analysis is as follows:
For a 1 * m matrix, that is, a series, maximal sub-rectangle can be obtained by finding the maximum long continuous string and dynamic programming ).
Then, for the n * m matrix, sum the numbers in each column to obtain a 1 * m matrix, the greatest sum obtained by using the method described above is the maximum submatrix sum in the submatrix where the number of all rows of the n * m matrix is n.
Then this question is enumerated by enumerating all rows as 1, 2, 3 ..... N matrix (brute force), respectively use the above method to compress the Matrix to obtain the maximum continuous string and find the maximum value, that is, the result.
My problem-solving code is as follows:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <string>#include <algorithm>using namespace std;int table[100][100];int sum[100];int N;int max_continuous_sum(){int maxs=0,s=0;for(int i=0; i<N; i++){if(s>=0) s+=sum[i];else s=sum[i];maxs = maxs>s ? maxs : s;}return maxs;}int main(){cin >> N;int maxsum=0;int tmp;for(int i=0; i<N; i++){for(int j=0; j<N; j++){cin >> table[i][j];sum[j]=table[i][j];}tmp = max_continuous_sum();maxsum = maxsum>tmp ? maxsum : tmp;for(int j=i-1; j>=0; j--){for(int k=0; k<N; k++)sum[k]+=table[j][k];tmp = max_continuous_sum();maxsum = maxsum>tmp ? maxsum : tmp;}}cout << maxsum << endl;return 0;}