Question link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 460 & page = show_problem & problem = 1696
The idea is the same as that of the two-dimensional largest submatrix, but it is changed to three-dimensional.
Enumerate the upper and lower bounds of the layers, and then "compress" all layers between the upper and lower bounds into one layer. On this layer, the two-dimensional method is used for calculation.
Use long
Code:
# Include <cstdio> # include <cstring> # include <iostream> # include <cctype> using namespace STD; typedef long int64; const int maxn = 25; int A, B, c; int64 cube [maxn] [maxn] [maxn]; inline int64 read_int64 () {char CH = getchar (); While (! Isdigit (CH) & Ch! = '-') CH = getchar (); bool neg = false; If (CH = '-') neg = true, CH = getchar (); int64 ans = 0; while (isdigit (CH) {ans = ans * 10 + CH-'0'; CH = getchar () ;}if (NEG) Return-ans; return ans ;} inline void input () {scanf ("% d", & A, & B, & C); memset (cube, 0, sizeof (cube )); for (int K = 1; k <= A; ++ K) {for (INT I = 1; I <= B; ++ I) {for (Int J = 1; j <= C; ++ J) {cube [k] [I] [J] = read_int64 (); cube [k] [I] [J] + = cube [k] [I] [J-1]-cube [k] [I-1] [J-1] + cube [k] [I-1] [J]-(cube [k-1] [I] [J-1]-cube [k-1] [I-1] [J-1] + cube [k-1] [I-1] [J]) + cube [k-1] [I] [J];} // column} // row} // layer} int64 solve () {// int64 ans =-2147483647-1; for (INT down = 0; down <A; ++ down) {for (INT up = down + 1; up <= A; ++ up) {// "row" on the enumeration layer for (int d = 0; D <B; ++ d) {for (INT u = d + 1; U <= B; ++ U) {int64 Minx = 0; For (int K = 1; k <= C; ++ K) {int64 sum = (cube [Up] [u] [k]-cube [Down] [u] [k]) -(cube [Up] [d] [k]-cube [Down] [d] [k]); ans = max (ANS, Sum-Minx ); if (sum <Minx) Minx = sum ;}}} return ans ;}int main () {int ncase; scanf ("% d", & ncase ); while (ncase --) {input (); cout <solve () <Endl; If (ncase) putchar ('\ n');} return 0 ;}