题目地址:http://poj.org/problem?id=1050

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

时间复杂度为O(N^2*M^2)

#include <stdio.h>
#include <limits.h> //PS[i][j]等于以(1, 1), (1, j), (i, 1), (i, j)为顶点的矩形区域的元素之和
void Preproccess(int matrix[101][101], int PS[101][101], int N){
int i, j;
for (i=0; i<=N; ++i){
PS[0][i] = 0;
PS[i][0] = 0;
}
for (i=1; i<=N; ++i){
for (j=1; j<=N; ++j){
PS[i][j] = PS[i-1][j] + PS[i][j-1] - PS[i-1][j-1] + matrix[i][j];
}
}
} int main(void){
int N;
int matrix[101][101];
int PS[101][101];
int i, j;
int i_min, i_max;
int j_min, j_max;
int max, tmp; while (scanf ("%d", &N) != EOF){
for (i=1; i<=N; ++i)
for (j=1; j<=N; ++j)
scanf ("%d", &matrix[i][j]);
Preproccess(matrix, PS, N);
max = INT_MIN;
/*以(i_min, j_min), (i_max, j_min), (i_min, j_max), (i_max, j_max)为顶点的矩形区域的元素之和,
等于PS[i_max][j_max] - PS[i_min-1][j_max] - PS[i_max][j_min-1] + PS[i_min-1][j_min-1]
*/
for (i_min=1; i_min<=N; ++i_min){
for (i_max=i_min; i_max<=N; ++i_max){
for (j_min=1; j_min<=N; ++j_min){
for (j_max=j_min; j_max<=N; ++j_max){
tmp = PS[i_max][j_max] - PS[i_min-1][j_max] - PS[i_max][j_min-1] + PS[i_min-1][j_min-1];
if (tmp > max)
max = tmp;
}
}
}
}
printf ("%d\n", max);
} return 0;
}

时间复杂度为O(N*M*min(N, M))

#include <stdio.h>
#include <limits.h> //PS[i][j]等于以(1, 1), (1, j), (i, 1), (i, j)为顶点的矩形区域的元素之和
void Preproccess(int matrix[101][101], int PS[101][101], int N){
int i, j;
for (i=0; i<=N; ++i){
PS[0][i] = 0;
PS[i][0] = 0;
}
for (i=1; i<=N; ++i){
for (j=1; j<=N; ++j){
PS[i][j] = PS[i-1][j] + PS[i][j-1] - PS[i-1][j-1] + matrix[i][j];
}
}
} //BC(PS, a, c, i)表示在第a行和第c行之间的第i列的所有元素的和,可以通过“部分和”PS[i][j]在O(1)时间内计算出来。
int BC(int PS[101][101], int a, int c, int i){
return PS[c][i] - PS[a-1][i] - PS[c][i-1] + PS[a-1][i-1];
} int MaxSum (int matrix[101][101], int PS[101][101], int N){
int max = INT_MIN;
int a, c, i;
int Start, All;
for (a=1; a<=N; ++a){
for (c=a; c<=N; ++c){
Start = BC(PS, a, c, N);
All = BC(PS, a, c, N);
for (i=N-1; i>=1; --i){
if (Start < 0)
Start = 0;
Start += BC(PS, a, c, i);
if (Start > All)
All = Start;
}
if (All > max)
max = All;
}
}
return max;
} int main(void){
int N;
int matrix[101][101];
int PS[101][101];
int i, j; while (scanf ("%d", &N) != EOF){
for (i=1; i<=N; ++i)
for (j=1; j<=N; ++j)
scanf ("%d", &matrix[i][j]);
Preproccess(matrix, PS, N);
printf ("%d\n", MaxSum (matrix, PS, N));
} return 0;
}

HDOJ上相似的题目:http://acm.hdu.edu.cn/showproblem.php?pid=1559

九度OJ上相似的题目:http://ac.jobdu.com/problem.php?pid=1492

参考资料:编程之美

POJ 1050 To the Max -- 动态规划的更多相关文章

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

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

  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(最大子矩阵之和)

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

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

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45906   Accepted: 24276 Desc ...

随机推荐

  1. java 学习基础学习单词及java关键词

    在JAVA学习中我们难免会犯一些逻辑错误,语法错误,和一些运行错误,对于英语不好的人,就的记下下面的2常用单词,有助于我们提高在使用软件编写代码的速度和代码调试,能更便捷的找出错误,知道1中的保溜关键 ...

  2. Python FTP多线程爆破脚本

    初学python, 自己编写了个FTP多线爆破小脚本代码很丑= = #!usr/bin/env python #!coding=utf-8 __author__='zhengjim' from ftp ...

  3. Spring技术内幕:Spring AOP的实现原理(二)

    **二.AOP的设计与实现 1.JVM的动态代理特性** 在Spring AOP实现中, 使用的核心技术时动态代理.而这样的动态代理实际上是JDK的一个特性.通过JDK的动态代理特性,能够为随意Jav ...

  4. Files to be needed by importing the android application with eclipse

    1. AndroidManifest.xml 2. project.properties # This file is automatically generated by Android Tools ...

  5. 10个android开源项目

    http://www.51testing.com/?uid-116228-action-viewspace-itemid-244285 1.Android团队提供的示例项目 如果不是从学习Androi ...

  6. Mysql命令行连接

    mysql在线参考手册地址: http://dev.mysql.com/doc/refman/5.1/zh/tutorial.html#connecting-disconnecting 在linux平 ...

  7. JavaScript网站设计实践(二)实现导航栏当前所选页面的菜单项高亮显示

    一.(一)中的代码还可以修改的地方. 在(一)中,如果是运行在服务器下,如apache等,可以把head和navigation的div抽取出来,放置在另一个html文件里,然后在页面中,include ...

  8. C#_delegate - combine function

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Dele ...

  9. 使用socket实现FTP程序

    #-*- coding:utf-8 -*- import socketserver from module import * class server: def __init__(self,reque ...

  10. 包括后台的Android美食APP项目开源代码

    项目简介 小食光定位为一款集美食,社交,LBS服务于一体的美食推荐APP.为你发现周边美食的同时提供一个吃货分享的平台. APP截图     功能模块 美食推荐 :提供基础的美食信息查询: 商家推荐  ...