http://poj.org/problem?id=1050

我们已经知道求最大子段和的dp算法 参考here  也可参考编程之美有关最大子矩阵和部分。

然后将这个扩大到二维就是这道题。顺便说一下,有时候不要把问题想复杂了,有些问题只能靠暴力求解,而这道题是暴力加算法。

在这个题中,我们可以把二维压缩到一维然后求解最大子段。我们先枚举所求矩阵的起点行和结束行,然后把每一列的数据之和求出,用这些数据和就构造出一个一维的数组(代码中我没有明确表示出这个数组),然后用最大子段和的dp算法求解。

关于二维压缩到一维的过程,适当处理可以大大减小时间复杂度。

最终时间复杂度是O(n^3);

我的代码,思路是正确的,但是提交上去之后wa了,求大神赐教。

//2013-06-26-08.45
//poj 1050
#include <iostream>
#include <string.h>
#include <algorithm>
int map[103][103];
int sum[103][103]; using namespace std; int main()
{
int n;
while (cin >> n)
{
memset(sum, 0, sizeof(sum));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> map[i][j];
sum[i][j] = map[i][j] + sum[i-1][j];
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < i; j++)
{
int tol = 0;
for (int k = 1; k <= n; k++)
{
if (sum[i][k]-sum[j][k] < 0) //这个地方就是压缩后的数组
tol = 0;
else
tol += (sum[i][k]-sum[j][k]);
if (tol > ans)
ans = tol;
}
}
}
cout << ans << endl;
}
return 0;
}

poj 1050 To the Max(最大子矩阵之和)的更多相关文章

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

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

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

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

  3. POJ 1050 To the Max (最大子矩阵和)

    题目链接 题意:给定N*N的矩阵,求该矩阵中和最大的子矩阵的和. 题解:把二维转化成一维,算下就好了. #include <cstdio> #include <cstring> ...

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

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

  5. hdu 1081 &amp; poj 1050 To The Max(最大和的子矩阵)

    转载请注明出处:http://blog.csdn.net/u012860063 Description Given a two-dimensional array of positive and ne ...

  6. [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)

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

  7. 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 ...

  8. POJ 1050 To the Max -- 动态规划

    题目地址:http://poj.org/problem?id=1050 Description Given a two-dimensional array of positive and negati ...

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

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

随机推荐

  1. HTML end~

    一.浏览器的兼容问题(关于浏览器的兼容问题 有很多大佬已经解释的很清楚了 这个得自己百度去多花点时间去了解 这里咱们只说一下前面的漏点) 浏览器兼容性问题又被称为网页兼容性或网站兼容性问题,指网页在各 ...

  2. python 基础学习笔记(2)---字符串功能函数

    **上一篇写到了,基本的数据类型,今天重点来讲一下字符串的功能函数**回顾一下上篇的内容:一.int 整型,在python 3 中与long型合并 可以达到 -9223372036854775808- ...

  3. java请求转发,响应重定向的区别

    请求转发:request.getRequestDispatcher().forward(); 例:request.getRequestDispatcher("/index.jsp" ...

  4. ifream子页面与父页面互调

    function a1(x){ alert('父页面:' + x); } function acc(){ var frames = document.getElementById("dial ...

  5. 【无线安全实践入门】网络扫描和ARP欺骗

    文中可能存在错误操作或错误理解,望大家不吝指正. 同时也希望可以帮助到想要学习接触此方面.或兴趣使然的你,让你有个大概的印象. !阅前须知! 本文是基于我几年前的一本笔记本,上面记录了我学习网络基础时 ...

  6. Win10自动更新关闭方法

    一.为什么很多人会选择禁用Win10自动更新? 1.win10自动更新定义: Win 10的自动更新功能,即 Windows Update.这项功能本意是为一些软件.漏洞等提供更新服务.一般来说,只要 ...

  7. Spring的<context:annotation-config>和<annotation-driven>

    <context:annotation-config>  相对于注册 AutowiredAnnotationBeanPostProcessor.CommonAnnotationBeanPo ...

  8. C语言学习书籍推荐《C专家编程Expert C Programming Deep C Secrets》下载

    Peter Van Der Linden (作者) <C和C++经典著作 C专家编程Expert C Programming Deep C Secrets>展示了C程序员所使用的编码技巧, ...

  9. 《Predict Anchor Links across Social Networks via an Embedding Approach》阅读笔记

    基本信息 文献:Predict Anchor Links across Social Networks via an Embedding Approach 时间:2016 期刊:IJCAI 引言 预测 ...

  10. Web前端_微信小程序实战开发

    微信小程序开发实战教程 一.微信小程序 它是一种混合开发的方式. 是安装在微信中的程序(一个程序最多2M空间). 1.1 注册 1  2 点击立即注册:进入下方页面 3  4 点击小程序进入表单填写页 ...