HDU 1559 maximum sub-matrix--dp--(maximum and sub-matrix models)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.