POJ 1050 To the Max (动态规划——求最大子矩阵和)
2010-01-20 03:17
477 查看
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
Sample Output
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
#include<iostream> using namespace std; int main() { int arr[110][110],arr2[110],dp[110],n,max; memset(arr,0,sizeof(arr)); while(cin >> n) { max = -2147483647; for(int i = 1;i <= n;++i) for(int j = 1;j <= n;++j) cin >> arr[i][j]; //动态规划过程,将二维数组的和加起来,转化为一维,再利用求最大连续子序列的方法DP求之 for(int i = 1;i <= n; ++i) { memset(arr2,0,sizeof(arr2)); for(int j = i; j <= n; ++j) //将矩阵从第i行加到第n行,每加一次算一次最大连续子序列一次 { memset(dp,0,sizeof(dp));//初始化DP很重要 for(int x = 1;x <= n; ++x) { arr2[x] += arr[j][x]; if(dp[x - 1] + arr2[x] > 0) //最大连续子段和的动态转移方程: { //DP(i) = 0 if(DP(i-1) + F(i) < 0) dp[x] = dp[x - 1] + arr2[x];// = DP(i - 1) + F(i) if(DP(i-1) + F(i) > 0) if(dp[x] > max) max = dp[x]; } else { dp[x] = 0; if(dp[x] > max) max = dp[x]; } } } } cout << max <<endl; } return 0; }[/code]
相关文章推荐
- POJ 1050 To the Max(动态规划、最大子矩阵和)
- [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)
- poj 1050 To the Max(最大子矩阵和)
- poj 1050 To the Max_dp求最大子矩阵和
- POJ 1050 To the Max(DP,最大子矩阵和)
- POJ 1050-To the Max(最大子矩阵和)
- G - To the Max POJ - 1050 最大子矩阵和
- poj 1050 To the Max_dp求最大子矩阵和
- POJ 1050 To the Max (最大子矩阵和)
- 动态规划(DP)——HDU1081、PKU1050 To The Max 最大子矩阵问题
- ZOJ 1074/POJ 1050 To the Max (最大子矩阵和)
- poj 1050 To the Max(最大子矩阵和)
- poj 1050 To the Max(最大子矩阵和)
- POJ 1050 To the Max 最大子矩阵和 简单dp
- hdu 1081 To The Max ****poj 1050(最大子矩阵和)DP
- poj 1050 To the Max && 51nod dp 最大子矩阵和
- POJ--1050--To the Max(线性动规,最大子矩阵和)
- [POJ 1050] To the Max DP+最大子矩阵和
- poj 1050 To the Max最大子矩阵和
- POJ 1050 To the Max (最大子矩阵和)