(高效算法设计)之高维问题 废料堆 Garbage heap Uva 10755



#include <iostream>
#include <algorithm>
#define FOR(i,s,p) for(int i=(s);i<=(p);i++)

using namespace std;

void expand(char i, bool b[]){
	b[0] = i & 1; i >>= 1;
	b[1] = i & 1; i >>= 1;
	b[2] = i & 1;
}
// 这里使用了二项式中的思想, 3个k

 char sign(bool b[]){
	return (b[0] + b[1] + b[2]) % 2 == 1 ? -1 : 1;
}

const int maxn = 30;
const long long INF = 1LL << 60;
long long S[maxn][maxn][maxn];

long long sum(int x1, int x2, int y1, int y2, int z1, int z2){
	int dx = x2 - x1 + 1, dy = y2 - y1 + 1, dz = z2 - z1 + 1;
	long long s = 0;
	bool b[3];
	for (int i = 0; i < 8; i++){
		expand(i, b);
		s += (S[x1 + dx*b[0]][y1 + dy*b[1]][z1 + dz*b[2]])*sign(b);
	}
	return s;
}

int main(){
	int T;
	cin >> T;
	while (T--)
	{
		int a, b, c;
			bool bb[3];
		cin >> a >> b >> c;
		memset(S, 0, sizeof(S));
		FOR(x, 1, a)FOR(y, 1, b)FOR(z, 1, c) cin >> S[x][y][z];
		FOR(x, 1, a)FOR(y, 1, b)FOR(z, 1, c) for (char i = 1; i <= 7;i++) {
			expand(i, bb);
			S[x][y][z] += S[x - bb[0]][y - bb[1]][z - bb[2]] * sign(bb);;
		}
		long long ans = -INF;
		long long M=0;
		FOR(x1, 1, a)FOR(x2, x1, a)FOR(y1, 1, b)FOR(y2, y1, b)FOR(z, 1, c){
			long long s = sum(x1, x2, y1, y2, 1, z);
			ans = max(s-M , ans);
			M = min(M, s);
		}
		cout << ans;
		if (T) cout << '
';

	}
	return 0;
}


原文地址:https://www.cnblogs.com/Pomodori/p/4316619.html