A problem that is simple to solve in one dimension is often much more difficult to solve in more than one dimension. Consider satisfying a boolean expression in conjunctive normal form in which each conjunct consists of exactly 3 disjuncts. This problem (3-SAT) is NP-complete. The problem 2-SAT is solved quite efficiently, however. In contrast, some problems belong to the same complexity class regardless of the dimensionality of the problem.
Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the subrectangle with the largest sum is referred to as the maximal sub-rectangle.
A sub-rectangle is any contiguous sub-array of size 1 × 1 or greater located within the whole array. 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-hand corner:
9 2
−4 1
−1 8
and has the 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 N2 integers separated by white-space (newlines and spaces). These N2 integers make up the array in row-major order (i.e., all numbers on the first row, left-to-right, then all numbers on 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
The output is 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
题意
给出一个n*n的矩阵,求最大的子矩阵的和。
题解
动态规划基础题,创建二维数组记录每个位置的最大和,减去重复部分即所求最大子序列。
例:二位数字dp[i][j]用来记录各位置的最大值,dp[i][j]+=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1] / /为什么要减一个dp[i-1][j-1] 因为加上dp[i-1][j]+dp[i][j-1]部分时,dp[i-1][j-1]部分被加上两次,故减去一次。
创建一个变量ans用来记录所求的最大子矩阵和。即 ans=max(ans,dp[i][j]-dp[x-1][j]-dp[i][y-1]+dp[x-1][y-1]) //为什么要加一个dp[x-1][y-1])呢,理由同上
解释x,y为何物:固定好i,j点后,x,y循环遍历一次表示从左上角开始一直到i,j的点,减去这个点覆盖的左上角矩阵,就是目前计算的矩阵。
关键代码
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
for (int x = 0; x <= i; x++)
{
for (int y = 0; y <= j; y++)
ans = max(ans, dp[i][j] - (x > 0 ? dp[x - 1][j] : 0) - (y > 0 ? dp[i][y - 1] : 0) + (x > 0 && y > 0 ? dp[x - 1][y - 1] : 0));
}
}
}
完整代码
/* UVA108 Maximum Sum
copyright by bamboo 2023.12.4
*/
#include <iostream>
#include <stdio.h>
using namespace std;
int dp[110][110]; // dp记录的是0.0到i.j的矩阵和
int N;
int main()
{
cin >> N;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cin >> dp[i][j];
}
}
// 开始动态规划
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (i == 0 && j == 0)
dp[i][j] = dp[i][j]; // 就是自己
else if (i == 0)
dp[i][j] += dp[i][j - 1]; // 第一行
else if (j == 0)
dp[i][j] += dp[i - 1][j]; // 第一列
else
dp[i][j] += dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]; // 其他
}
}
// 开始算最大值
int ans = -127;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
for (int x = 0; x <= i; x++)
{
for (int y = 0; y <= j; y++)
ans = max(ans, dp[i][j] - (x > 0 ? dp[x - 1][j] : 0) - (y > 0 ? dp[i][y - 1] : 0) + (x > 0 && y > 0 ? dp[x - 1][y - 1] : 0));
}
}
}
cout << ans << endl;
}
最后看看草稿…
(画的图和是实际写的代码有一些不同,不过思路都没问题~)