HDU 1559 最大子矩陣(DP)

來源:互聯網
上載者:User
最大子矩陣

Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2249    Accepted Submission(s): 1139


Problem Description給你一個m×n的整數矩陣,在上面找一個x×y的子矩陣,使子矩陣中所有元素的和最大。 


Input輸入資料的第一行為一個正整數T,表示有T組測試資料。每一組測試資料的第一行為四個正整數m,n,x,y(0<m,n<1000 AND 0<x<=m AND 0<y<=n),表示給定的矩形有m行n列。接下來這個矩陣,有m行,每行有n個不大於1000的正整數。 


Output對於每組資料,輸出一個整數,表示子矩陣的最大和。 


Sample Input

14 5 2 23 361 649 676 588992 762 156 993 169662 34 638 89 543525 165 254 809 280
 


Sample Output

2474
 


Authorlwg 


SourceHDU 2006-12 Programming Contest 


RecommendLL 這個題目說是DP其實有點牽強,直接暴力就可以搞定,而且好比不確定維數的要好算一些

#include <stdio.h>#include <string.h>#include <stdlib.h>int map[1005][1005];int rec[1005];int my_rec[1005];int n,m,x,y;int cal(int i,int j,int k)//第k列的i到j行之和{int ans=0;for(i;i<=j;i++)ans+=map[i][k];return ans;}int dp(){int i,j,k,r;int ans=0;int temp;for(i=0;i+x-1<n;i++){temp=0;for(k=0;k<m;k++)rec[k]=cal(i,i+x-1,k);for(k=0;k<y;k++)temp+=rec[k];if(temp>ans)ans=temp;for(k=y;k<m;k++){temp-=rec[k-y];temp+=rec[k];if(ans < temp)ans=temp;}}return ans;}int main(){int i,j,k;int t;scanf("%d",&t);while(t--){scanf("%d%d%d%d",&n,&m,&x,&y);if(n<x || m<y){printf("0\n");continue;}for(i=0;i<n;i++)for(j=0;j<m;j++)scanf("%d",&map[i][j]);printf("%d\n",dp());}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.