转换为一维数组求最大连续子段和,然后dp水。
#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int MAXN = 111; int dp[MAXN], mat[MAXN][MAXN]; int subMax(int n, int len){ int ret = mat[n][0]; dp[0] = mat[n][0]; for(int i = 1;i < len;i ++){ if(dp[i-1] > 0) dp[i] = mat[n][i] + dp[i-1]; else dp[i] = mat[n][i]; ret = max(ret, dp[i]); } return ret; } int main(){ int n; int tmat[MAXN]; //freopen("in.cpp", "r", stdin); while(~scanf("%d", &n)){ for(int i = 0;i < n;i ++){ for(int j = 0;j < n;j ++){ scanf("%d", &mat[i][j]); } } int ans = -0x7fffffff; for(int i = 0;i < n;i ++){ ans = max(subMax(i, n), ans); for(int j = i+1;j < n;j ++){ for(int k = 0;k < n;k ++) mat[i][k] += mat[j][k]; ans = max(subMax(i, n), ans); } } cout << ans << endl; } }