The meaning of the question is very simple. Given a large matrix of N * n, calculate the value and the largest submatrix.
At first, I couldn't find the DP, and there was no optimal sub-structure. Later, Cong gave me the idea and turned it into a one-dimensional model, which turned into a maximum continuous sum. To convert the data into one dimension, the width of the sub-matrix must be enumerated. Through the pre-processing suffix, the sum of a certain segment of each column can be quickly calculated, and then one-dimensional DP can be performed .. The total complexity is O (n ^ 3 );
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int mat[110][110];int suffix[110][110];int n;int main(){ while (scanf("%d",&n)!=EOF) { for (int i=1; i<=n; i++) { for (int j=1; j<=n; j++) { scanf("%d",&mat[i][j]); } } //memset(suffix,0,sizeof suffix); for (int i=1; i<=n; i++) { for (int j=1; j<=n; j++) { suffix[i][j]=suffix[i][j-1]+mat[i][j]; } } int d=0; int ans=0; for (int i=1; i<=n; i++) { for (int j=i; j<=n; j++) { d=0; for (int k=1; k<=n; k++) { int tmp=suffix[k][j]-suffix[k][i-1]; if (d+tmp>tmp) d+=tmp; else d=tmp; ans=max(d,ans); } } } printf("%d\n",ans); } return 0;}