题目链接:http://poj.org/problem?id=1050

思路分析:

该题目为经典的最大子矩阵和问题,属于线性dp问题;最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而最大子矩阵为二维问题,

可以考虑将二维问题转换为一维问题,即变为最大子段和问题即可求解;

先考虑暴力解法,暴力解法需要枚举子矩阵的左上角元素的坐标与子矩阵的右下角坐标即可枚举所有的子矩阵;对于每个子矩阵,考虑压缩子矩阵的每一列

元素,即求每一列的元素的和,这样子矩阵就转换为一维的情况,再使用最大子段和问题,即可决定出在子矩阵的列决定的情况下,求出使子矩阵和最大的

行的选择;最后需要枚举子矩阵所有列的情况即可;

代码如下:

#include <iostream>
using namespace std; const int MAX_N = + ;
int sum[MAX_N];
int matrix[MAX_N][MAX_N]; int main()
{
int n, ans; while (scanf("%d", &n) != EOF)
{
for (int i = ; i < n; ++i)
for (int j = ; j < n; ++j)
scanf("%d", &matrix[i][j]); ans = INT_MIN;
for (int top = ; top < n; ++top)
{
for (int buttom = top; buttom < n; ++buttom)
{
int t_sum = , t_ans = INT_MIN; for (int i = ; i < n; ++i)
{
sum[i] = ;
for (int k = top; k <= buttom; ++k)
sum[i] += matrix[k][i];
if (t_sum >= )
t_sum += sum[i];
else
t_sum = sum[i]; if (t_sum > t_ans)
t_ans = t_sum;
}
if (t_ans > ans)
ans = t_ans;
}
}
printf("%d\n", ans);
}
return ;
}

poj 1050 To the Max(线性dp)的更多相关文章

  1. poj - 1050 - To the Max(dp)

    题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...

  2. poj 1050 To the Max (简单dp)

    题目链接:http://poj.org/problem?id=1050 #include<cstdio> #include<cstring> #include<iostr ...

  3. POJ 1050 To the Max 枚举+dp

    大致题意: 求最大子矩阵和 分析: 一开始想复杂了,推出了一个状态方程:d[i][j]=max(d[i][j-1]+-,d[i-1][j]+-).写着写着发现上式省略的部分记录起来很麻烦. 后来发现n ...

  4. [poj]1050 To the Max dp

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  5. POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)

    传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  6. poj 1050 To the Max(最大子矩阵之和)

    http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here  也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...

  7. poj 1050 To the Max 最大子矩阵和 经典dp

    To the Max   Description Given a two-dimensional array of positive and negative integers, a sub-rect ...

  8. poj 1050 To the Max(最大子矩阵之和,基础DP题)

    To the Max Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 38573Accepted: 20350 Descriptio ...

  9. POJ 1050 To the Max 暴力,基础知识 难度:0

    http://poj.org/problem?id=1050 设sum[i][j]为从(1,1)到(i,j)的矩形中所有数字之和 首先处理出sum[i][j],此时左上角为(x1,y1),右下角为(x ...

随机推荐

  1. js 中的闭包

    /** *闭包就是在一个函数的外面访问函数内部的变量 **/ var name = "xiao A"; var obj = { name : "xiao B" ...

  2. A - FatMouse' Trade

    Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the wareho ...

  3. ie7(z-index)

    父级元素加上position:relative;并设置z-index. 父级元素的z-index优先,子元素的z-index是相对于父级元素的index. <div style="po ...

  4. delphi 7 下安装 indy 10.5.8 教程

    本教程用 indy 10.5.8 替换 delphi 7 自带的 indy 版本,让大家深入了解 delphi 组件安装的方法. 第一步:下载 indy 10.5.8 组件,解压到合适的目录里.如 D ...

  5. 让.net程序自动运行在管理员权限下

    原文:让.net程序自动运行在管理员权限下 如何让.net程序自动运行在管理员权限下 VS2010 c# 编译的WINFORM程序 在Win7 以管理员身份运行 windows 7和vista提高的系 ...

  6. RIA Service 的 SOAP EndPoint

    原文 www.cyqdata.com/cnblogs/article-detail-39983-english 越来越多的朋友都在使用Silverlight开发应用程序,其中我们常用的还会有一个特殊的 ...

  7. Linux常用的系统监控shell脚本

    http://www.linuxqd.com下面是我常用的几个Linux系统监控的脚本,大家可以根据自己的情况在进行修改,希望能给大家一点帮助.1.查看主机网卡流量 #!/bin/bash #netw ...

  8. C# 保留小数点后两位(方法总结)

    最简单使用: float i=1.6667f; string show=i.ToString("0.00"); //结果1.67(四舍五入) 其他类似方法: string show ...

  9. 动态调用python类和函数

    类 class test1(object): def __init__(self): print "i am test1" class test2(object): def __i ...

  10. iOS 面试题:OC基本概念题

    1.什么是类和对象? 类是一组具有同样特征和功能的事物的抽象 对象描写叙述了一个物体的特征和行为实现 类是对象的抽象 对象是类的实例 2.OC中定义类,创建对象,使用对象. OC中定义类分为接口部分, ...