最大子序列和(二维数组)
参考:https://blog.csdn.net/MashiroSky/article/details/52191822
AC:
//动态规划,最大子序列和
#include <iostream>
#include <climits>
using namespace std;
int n, a[1010][1010], sum[1010][1010];
int main() {
int i, j;
cin >> n;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
cin >> a[i][j];
sum[i][j] = a[i][j] + sum[i-1][j];//转化为一维数组
}
}
int ans = INT_MIN;
for (i = 1; i <= n; i++) {//从i行
for (j = i; j <= n; j++) {//到j行
int s = 0;
for (int k = 1; k <= n; k++) {//从1-n列
s += sum[j][k] - sum[i][k];
if (s < 0) s = 0;
else ans = max(ans, s);
}
}
}
cout << ans << endl;
return 0;
}