Test instructions: Given a matrix, find the largest and most large sub-matrices with a width of a and a length of B.
Analysis: Direct use of the maximum and sub-matrix formula, but here is a more restrictive conditions, so in the sum of a little bit different, is not row by line and a column to seek, but each with a A, a, a span to seek
Dp[k]=mt[i][k]+....+mt[i+a][k], and then each time Sum=dp[k]+.....+dp[k+b],mx=max (mx,sum)
Code:
#include <iostream> #include <cstring>using namespace Std;int m,n,a,b,t;int mt[1005][1005];int mx,sum,dp[ 1005];int Max (int i,int j) {return i>j?i:j;} void DP () {mx=0;for (int i=0;i<m;i++) {memset (dp,0,sizeof (DP)); for (int. j=0;j<a&&i+j<m;j++) {for (int k) =0;k<n;k++) {dp[k]+=mt[i+j][k];}} sum=0;for (int k=0;k<b;k++) Sum+=dp[k];mx=max (mx,sum); for (int k=b;k<n;k++) {Sum-=dp[k-b];sum+=dp[k];mx=max (mx , sum);}}} int main () { cin>>t; while (t--) {cin>>m>>n>>a>>b; for (int. i=0;i<m;i++) for ( int j=0;j<n;j++) cin>>mt[i][j]; DP (); cout<<mx<<endl; }}
HDU 1559 maximum sub-matrix--dp--(maximum and sub-matrix models)