第一次做三维的。
思路:
先枚举正视图的上下边界,再枚举左视图的左右边界,最后在正视图的左到右做一次最大连续和。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vll;
const int MAXN = 1e6 + 10;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const double eps = 1e-8;
int n, m, k;
vector< vector<vll> > mat, sum;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
int times;
cin >> times;
while(times--){
cin >> n >> m >> k;
mat.clear();
mat.resize(n + 2, vector<vll>(m + 2, vll(k + 2)));
sum.clear();
sum.resize(n + 2, vector<vll>(m + 2, vll(k + 2)));
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
for(int t = 1; t <= k; t++)
cin >> mat[i][j][t];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
for(int t = 1; t <= k; t++)
sum[i][j][t] = sum[i - 1][j][t] + mat[i][j][t];
ll res = -INF * INF;
for(int i = 1; i <= n; i++)
for(int j = i; j <= n; j++)
for(int t = 1; t <= m; t++)
for(int p = t; p <= m; p++){
ll cur = 0;
for(int q = 1; q <= k; q++){
ll s = 0;
for(int r = t; r <= p; r++)
s += sum[j][r][q] - sum[i - 1][r][q];
cur = max(s, cur + s);
res = max(res, cur);
}
}
cout << res << endl;
if(times)
cout << endl;
}
cerr << "execute time : " << (double)clock() / CLOCKS_PER_SEC << endl;
return 0;
}
未来可期。