转载请注明出处:http://blog.csdn.net/u012860063

Description

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the
sub-rectangle with the largest sum is referred to as the maximal sub-rectangle. 

As an example, the maximal sub-rectangle of the array: 



0 -2 -7 0 

9 2 -6 2 

-4 1 -4 1 

-1 8 0 -2 

is in the lower left corner: 



9 2 

-4 1 

-1 8 

and has a sum of 15. 

Input

The input consists of an N * N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N^2 integers separated by whitespace (spaces and newlines).
These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].

Output

Output the sum of the maximal sub-rectangle.

Sample Input

4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1 8 0 -2

Sample Output

15

Source

题意:给你一个N*N的矩阵,求当中和最大的子矩阵的值!

代码例如以下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int i, j, k, t;
int a, sum, max, N, m[147][147];
while(~scanf("%d",&N))
{
memset(m,0,sizeof(m));
for(i = 1; i <= N; i++)
{
for(j = 1; j <= N; j++)
{
scanf("%d",&a);
m[i][j]+=m[i][j-1]+a;//表示第i行前j个数之和
}
}
max = -128;
for(i = 1; i <= N; i++)//起始列
{
for(j = i; j <= N; j++)//终止列
{
sum = 0;
for(k = 1; k <= N; k++)//对每一行进行搜索
{
if(sum < 0)
sum = 0;
sum+=m[k][j]-m[k][i-1];
//m[k][j]-m[k][i-1]表示第k行第i列之间的数
if(sum > max)
max = sum;
}
}
}
printf("%d\n",max);
}
return 0;
}

hdu 1081 &amp; poj 1050 To The Max(最大和的子矩阵)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  7. poj 1050 To the Max(线性dp)

    题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...

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

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

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

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

随机推荐

  1. [译]pycache是什么?

    原回答: https://stackoverflow.com/questions/16869024/what-is-pycache 当你用python运行一个程序时,解释器首先将它编译成字节码(这是一 ...

  2. springmvc始终跳转至首页,不报404错误

    本篇博客特别补充:2017-3-4 9:42,经过分析和测试,本篇博客的解决方案只是碰巧,暂时的解决了问题.在后续的运行中,又出现了同样的毛病.经过日志跟踪,发现了端倪,下篇博客深入的剖析!本篇博客, ...

  3. HDU——1874畅通工程续(Dijkstra与SPFA)

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  4. MySQL-MongoDB开源监控利器之PMM

    背景说明: PMM是percona公司提供的一个对于MySQL和MongoDB的监控和管理平台.PMM有两部分组成PMM Client和PMM Server PMM Client:安装在每一台需要进行 ...

  5. vue-cli3 vue.config.js配置

    module.exports = { // 基本路径 baseUrl: '', // 输出文件目录 outputDir: 'dist', // eslint-loader 是否在保存的时候检查 lin ...

  6. 2017 UESTC Training for Data Structures-解题报告

    题目链接:http://acm.uestc.edu.cn/#/contest/show/155 这个数据结构训练主要针对线段树,树转数组和并查集.比较适合刚入门数据结构的同学. 注意,因为后面题的代码 ...

  7. 更改 terminal 開啟時,預設的路徑

    echo "cd /media" >> ~/.bashrc open a new terminal (ctrl + alt + t)

  8. iOS8下定位问题解决

    项目是以前iOS7的,升级iOS8后,无法成功获取用户位置.后来才发现iOS8 使用定位需要在infoplist文件中加2个key,然后manager需要加一个方法,指定定位授权机制   在plist ...

  9. Vue.js实战:初识Vue.js

    一.Vue.js是什么 简单小巧的核心,渐进式技术栈,足以应付任何规模的应用. 简单小巧指的是Vue.js 压缩后大小仅有17KB 所谓渐进式(Progressive)就是你一步一步,有阶段性地来使用 ...

  10. man

    Description n间房子高度不同,Man 要从最矮的房子按照高度顺序跳到最高的房子,你知道房子的顺序,以及Man一次最远可以跳多远,相邻的房子至少有1的距离,房子的宽不计,现在由你安排相邻房子 ...