uva 10051
将每一个分解为六个两面的 简单地dp 回溯输出路径.....
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; struct cc { int top,bom,fa,w; void f(int a, int b, int c, int d) { top = a; bom = b; fa = c; w = d; } }; cc cube[3100]; char face[7][10] = { "front","back", "left", "right", "top", "bottom"}; int dp[3100],cu[7],p[3100]; void printpath(int k) { if(k == -1) return ; printpath(p[k]); printf("%d %s\n",cube[k].w, face[cube[k].fa]); } int main() { int n, ca = 1; while(scanf("%d", &n) == 1 && n) { int m = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < 6; j++) scanf("%d",&cu[j]); for(int j = 0; j < 6; j++) { cube[m++].f(cu[j], j%2 == 1?cu[j-1]:cu[j+1], j, i+1); } } memset(dp, 0, sizeof( dp)); memset(p, -1, sizeof( p)); int _max = 0, k; for(int i = 0; i < m; i++) for(int j = i+1; j < m; j++) if(cube[j].w > cube[i].w && cube[i].bom == cube[j].top && dp[j] < dp[i]+1) { dp[j] = dp[i]+1; p[j] = i; if(dp[j] > _max) { _max = dp[j]; k = j; } } printf("Case #%d\n%d\n",ca++,_max+1); printpath(k); puts(""); } return 0; }