NYOJ 104 最大子矩阵(二维DP)

2015-01-27 10:15:14 · 作者: · 浏览: 14

最大和

时间限制:1000 ms | 内存限制:65535 KB 难度:5
描述

给定一个由整数组成二维矩阵(r*c),现在需要找出它的一个子矩阵,使得这个子矩阵内的所有元素之和最大,并把这个子矩阵称为最大子矩阵。
例子:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
其最大子矩阵为:

9 2
-4 1
-1 8
其元素总和为15。

输入
第一行输入一个整数n(0 每组测试数据:
第一行有两个的整数r,c(0 随后有r行,每行有c个整数;
输出
输出矩阵的最大子矩阵的元素之和。
样例输入
1
4 4
0 -2 -7 0 
9 2 -6 2 
-4 1 -4 1 
-1 8 0 -2 
样例输出
15

#include
      
         
#include
       
         using namespace std; #define N 110 int a[N][N]; int b[N]; int main(){ int n,r,c,i,j,k; cin>>n; while(n--){ cin>>r>>c; for(i=1;i<=r;++i) for(j=1;j<=c;++j) { cin>>a[i][j]; a[i][j]+=a[i-1][j]; } int max=a[1][1]; for(i=0;i<=r-1;++i) for(j=i+1;j<=r;++j) { memset(b,0,sizeof(b)); for(k=1;k<=c;++k) { if(b[k-1]>=0) b[k]=b[k-1]+a[j][k]-a[i][k]; else b[k]=a[j][k]-a[i][k]; if(max
        
         


<script type="text/java script">
<script type="text/java script">BAIDU_CLB_fillSlot("771048");
点击复制链接 与好友分享! 回本站首页
<script> function copyToClipBoard(){ var clipBoardContent=document.title + '\r\n' + document.location; clipBoardContent+='\r\n'; window.clipboardData.setData("Text",clipBoardContent); alert("恭喜您!复制成功"); }
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
您对本文章有什么意见或着疑问吗?请到 论坛讨论您的关注和建议是我们前行的参考和动力??
上一篇: LeetCode算法编程 - Palindrome Partitioning
下一篇: 数据结构与算法之递推算法 C++与PHP实现
相关文章
<script type="text/java script">BAIDU_CLB_fillSlot("182716");
<script type="text/java script">BAIDU_CLB_fillSlot("517916");
图文推荐
<iframe src="http://www.2cto.com/uapi.php?tid=348918&catid=339&title=TllPSiAxMDQg1 6089fTvtjV86Ootv7OrERQo6k=&forward=http://www.2cto.com/kf/201411/348918.html" width="100%" height="100%" id="comment_iframe" name="comment_iframe" frameborder="0" scrolling="no">
<script type="text/java script">BAIDU_CLB_fillSlot("771057");