这个题目是一个LIS的变形,题目的阶段很明显,就是按照cube的重量分阶段
每个阶段的状态:dp[i][k] 表示第i个cube第k个面朝上的最大长度
状态转移方程你:dp[i][k] = 1+max{dp[j][z]}, (n >= j > i) (1 <= z,k <= 6)
ans = max(dp[i][k]), (1 <= i <= n)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define FACE 6
#define MAXN 501
char conver[][FACE+1] = {
{"front"}, {"back"}, {"left"}, {"right"}, {"top"}, {"bottom"}
};
typedef struct NODE_ {
int idx, face_idx;
}NODE;
NODE pre[MAXN][FACE];
int dp[MAXN][FACE], color[MAXN][FACE];
void back_trace(const int &idx, const int &face_idx)
{
if( -1 == idx ) {
return;
}
printf("%d %s\n", idx+1, conver[face_idx]);
back_trace(pre[idx][face_idx].idx, pre[idx][face_idx].face_idx);
}
int dynamic_programming(const int &n)
{
int rst(0), idx, face_idx, cur_bottom;
memset(dp, 0, sizeof(dp)); memset(pre, -1, sizeof(pre));
for(int i = n-1; i >= 0; i --) {
for(int k = 0; k < FACE; k ++) {
cur_bottom = (k&1)? k-1 : k+1; dp[i][k] = 1;
for(int j = n-1; j > i; j --) {
for(int z = 0; z < FACE; z ++) {
if( color[i][cur_bottom] == color[j][z] && dp[i][k] < dp[j][z]+2 ) {
dp[i][k] = dp[j][z]+1; pre[i][k].idx = j; pre[i][k].face_idx = z;
if( rst < dp[i][k] ) {
rst = dp[i][k]; idx = i; face_idx = k;
}
}
}
}
}
}
printf("%d\n", rst); back_trace(idx, face_idx);
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int n, cas(1);
while( scanf("%d", &n) && n ) {
for(int i = 0; i < n; i ++) {
for(int j = 0; j < FACE; j ++) {
scanf("%d", &color[i][j]);
}
}
printf("Case #%d\n", cas ++); dynamic_programming(n); printf("\n");
}
return 0;
}