题目链接:http://acm.swust.edu.cn/problem/589/
2 3 4
4 1 2 8
0 5 -48 4
3 0 1 9
2 1 4 9
1 0 1 7
3 1 2 8
|
45 |
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 #define maxn 51 5 #define inf 0x3f3f3f3f 6 int h, m, n, ans; 7 int sum[maxn][maxn][maxn], x[maxn][maxn][maxn], dp[maxn][maxn][maxn][maxn]; 8 //sum前h层,前i行,第j列的权值之和,dp数组三维压缩成二维后sx,sy点到ex,ey点构成平面矩形最值 9 10 int main(){ 11 cin >> h >> m >> n; 12 for (int i = 1; i <= h; i++) 13 for (int j = 1; j <= m; j++) 14 for (int k = 1; k <= n; k++){ 15 cin >> x[i][j][k]; 16 sum[i][j][k] = sum[i][j - 1][k] + sum[i][j][k - 1] - sum[i][j - 1][k - 1] + x[i][j][k]; 17 } 18 for (int sx = 1; sx <= m; sx++) 19 for (int sy = 1; sy <= n; sy++) 20 for (int ex = sx; ex <= m; ex++) 21 for (int ey = sy; ey <= n; ey++){ 22 dp[sx][sy][ex][ey] = -inf; 23 for (int i = 1; i <= h; i++){ 24 //ptr压缩出来的矩阵块权值和 25 int ptr = sum[i][ex][ey] - sum[i][sx - 1][ey] - sum[i][ex][sy - 1] + sum[i][sx - 1][sy - 1]; 26 dp[sx][sy][ex][ey] = max(dp[sx][sy][ex][ey] + ptr, ptr); 27 ans = max(dp[sx][sy][ex][ey], ans); 28 } 29 } 30 cout << ans << endl; 31 return 0; 32 }