poj1050 To the Max (动态规划)
来源:互联网 发布:网络url地址是什么 编辑:程序博客网 时间:2024/06/05 07:27
题目意思:
给出一个矩阵。求出和最大的子矩阵,在解决这个问题的之前,首先看一下这个问题的一维问题,给出一个序列求最大子序列。满足i<=i<=j<=n 求出最大的i-->j的和。
题目分析:
对于一维问题,有很多的解决方法,当然也对应不同的时间和空间复杂度。有暴力,优化暴力,贪心,动态规划等解法,由于这里此题的二维问题要用到动态规划,这里只给出动态规划算法。对于二维问题只需要转化为一维的问题,在用动态规划方法解决问题。
一维动归:
int ToMax(int a[],int n){ int s[10000]={0}; for(int i=1;i<=n;i++){ if(s[i-1]>=0) s[i]=s[i-1]+a[i]; else s[i]=a[i]; } int ma=-10000000; for(int i=1;i<=n;i++){ if(s[i]<ma) ma=s[i]; } return ma;}
二维AC代码:
#include<iostream>#include<cstring>using namespace std;int a[105][105],sum[105],b[105];int main(){ int n; while(cin>>n){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cin>>a[i][j]; } } memset(sum,0,sizeof(sum)); int max=-100000; for(int i=1;i<=n;i++){ memset(sum,0,sizeof(sum)); memset(b,0,sizeof(b)); for(int k=i;k<=n;k++){ for(int j=1;j<=n;j++){ b[j]+=a[k][j]; if(sum[j-1]>=0){ sum[j]=sum[j-1]+b[j]; } else sum[j]=b[j]; if(max<sum[j]) max=sum[j]; } } } cout<<max<<endl; } return 0;}
0 0
- poj1050 To the Max (动态规划)
- POJ1050 To the Max 动态规划
- POJ1050 To the Max 简单动态规划
- POJ1050 To the Max
- POJ1050 To the Max
- POJ1050 To the Max
- poj1050 to the max
- poj1050 To the Max
- poj1050 To the Max
- POJ1050--To the Max
- POJ1050 To the Max
- poj1050 To the Max
- poj1050:to the max
- POJ1050 To the Max
- POJ1050 To the Max
- 【poj1050】 To the Max
- POJ1050-To the Max
- poj1050 to the Max
- 关于计算机视觉的随谈
- 机器学习问题的十个实例
- C++读书笔记
- 【Leetcode】Symmetric Tree (Tree Judge)
- PHPStorm使用经验
- poj1050 To the Max (动态规划)
- Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
- 【Leetcode】Balanced Binary Tree (Tree Judge)
- 关于推荐系统中的特征工程
- Servlet总结
- 第十一周项目6-回文、素数(输出1000以内所有回文数)
- 第十一周项目六—素数
- 【UML】寒冬伊始,初窥门径
- Spring单元测试