简单的DP
状态是h[i][j], 代表顶面是第i块的第j面时的最高高度
状态转移很简单
#include <iostream> using namespace std; /* #include <fstream> ifstream fin("data.txt"); #define cin fin */ int n; int blocks[31][3]; int faces[3][3] = {{0,1, 2}, {1, 2, 0}, {0, 2, 1}}; int h[31][3]; void dp() { int i, j, k, t, cura, curb, maxh=0; bool finish; for (t=1; t<=3*n; t++) { finish = true; for (i=1; i<=n; i++) for (j=0; j<3; j++) { //当前状态h[i][j] cura = blocks[i][faces[j][0]]; curb = blocks[i][faces[j][1]]; for (k=1; k<=n; k++) { if ((cura>blocks[k][0]&&curb>blocks[k][1]) || (cura>blocks[k][1]&&curb>blocks[k][0])) { if (h[k][0] < h[i][j] + blocks[k][2]) { h[k][0] = h[i][j] + blocks[k][2]; finish = false; } } if ((cura>blocks[k][1]&&curb>blocks[k][2]) || (cura>blocks[k][2]&&curb>blocks[k][1])) { if (h[k][1] < h[i][j] + blocks[k][0]) { h[k][1] = h[i][j] + blocks[k][0]; finish = false; } } if ((cura>blocks[k][0]&&curb>blocks[k][2]) || (cura>blocks[k][2]&&curb>blocks[k][0])) { if (h[k][2] < h[i][j] + blocks[k][1]) { h[k][2] = h[i][j] + blocks[k][1]; finish = false; } } } } if (finish) break; } for (maxh=0, i=1; i<=n; i++) { for (j=0; j<3; j++) { if (h[i][j] > maxh) maxh = h[i][j]; } } cout << maxh << endl; } int main() { int i, count = 0; while (cin>>n && n) { memset(h, 0, sizeof(h)); for (i=1; i<=n; i++) { cin >> blocks[i][0] >> blocks[i][1] >> blocks[i][2]; h[i][0] = blocks[i][2]; h[i][1] = blocks[i][0]; h[i][2] = blocks[i][1]; } cout << "Case " << ++count << ": maximum height = "; dp(); } return 0; }