1、http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=992
2、题目
Problem A: Tower of Cubes
In this problem you are given
N
colorful cubes each having a distinct weight. Each face of a cube is colored with one color. Your job is to build a tower using the cubes you have subject to the following restrictions:
Problem A: Tower of Cubes |
- Never put a heavier cube on a lighter one.
- The bottom face of every cube (except the bottom cube, which is lying on the floor) must have the same color as the top face of the cube below it.
- Construct the tallest tower possible.
Input
The input may contain multiple test cases. The first line of each test case contains an integer N ( ) indicating the number of cubes you are given. The i ?th ( ) of the next N lines contains the description of the i ?th cube. A cube is described by giving the colors of its faces in the following order: front, back, left, right, top and bottom face. For your convenience colors are identified by integers in the range 1 to 100. You may assume that cubes are given in the increasing order of their weights, that is, cube 1 is the lightest and cube N is the heaviest.The input terminates with a value 0 for N.
Output
For each test case in the input first print the test case number on a separate line as shown in the sample output. On the next line print the number of cubes in the tallest tower you have built. From the next line describe the cubes in your tower from top to bottom with one description per line. Each description contains an integer (giving the serial number of this cube in the input) followed by a single white?space character and then the identification string (front, back, left, right, top or bottom) of the top face of the cube in the tower. Note that there may be multiple solutions and any one of them is acceptable.
Print a blank line between two successive test cases.
Sample Input
3 1 2 2 2 1 2 3 3 3 3 3 3 3 2 1 1 1 1 10 1 5 10 3 6 5 2 6 7 3 6 9 5 7 3 2 1 9 1 3 3 5 8 10 6 6 2 2 4 4 1 2 3 4 5 6 10 9 8 7 6 5 6 1 2 3 4 7 1 2 3 3 2 1 3 2 1 1 2 3 0
Sample Output
Case #1 2 2 front 3 frontCase #2 8 1 bottom 2 back 3 right 4 left 6 top 8 front 9 front 10 top
3、看的网上代码
#include <iostream>
#include <cstdio>
#include <string.h>
#include <cmath>
using namespace std;char face[6][10] = { "front","back", "left", "right", "top", "bottom"};
//dp用来记录最大高度,p记录该立方体的上面的立方体的编号,cu存6个面的颜色
//top,bot记录立方体的上下两面的颜色,num记录重量,df记录那个面位于top位置
int dp[3001],p[3001],cu[6],top[3001],bot[3001],num[3001],df[3001];
int n, m;
void printpath( int k) //递归打印路径
{if( k==-1)return ;printpath( p[k] );printf("%d %s\n",num[k], face[df[k]]);
}
int main()
{int t=1,first =1;while( scanf("%d", &n)==1 && n){if( !first)printf("\n");elsefirst =0;m=0;int i,j;for( i=0; i<n; i++){for( j=0; j<6; j++)scanf("%d",&cu[j]);for( j=0; j<6; j++) //分解为6个立方体{df[m] =j;num[m] =i+1;top[m] =cu[j];if( j%2)bot[m] =cu[j-1];elsebot[m] =cu[j+1];m++;}}memset( dp, 0, sizeof( dp));memset( p, -1, sizeof( p));for( i=0; i<m; i++)for( j=i+1; j<m; j++)if( num[j]> num[i] && bot[i] ==top[j] && dp[j]< dp[i]+1){dp[j] =dp[i]+1;p[j] =i;}int max =0,k=0;for( i=1; i<m; i++)if( dp[i]>max){max =dp[i];k =i;}printf("Case #%d\n%d\n",t++,max+1);printpath(k );}return 0;
}
错的代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define N 500
char face[6][10]={"front","back","left","right","top","bottom"};int n,ans,ansi,ansf,a[N][6],dp[N][6],last[N][6],lastface[N][6];void print(int i,int f){if (i==-1) return;print(last[i][f],lastface[i][f]);printf("%d %s\n",i+1,face[f]);
}int main(){//freopen("output.txt","w",stdout);int T=0,first=1;while (scanf("%d",&n),T++,n){//if (T>1) printf("\n");if( !first)printf("\n");elsefirst =0;ans=0;for (int i=0;i<n;i++){for (int j=0;j<6;j++)scanf("%d",&a[i][j]);for (int j=0;j<6;j++){last[i][j]=lastface[i][j]=-1;int oface=(j&1)==1?j-1:j+1,best=0;for (int ii=0;ii<i;ii++)for (int jj=0;jj<6;jj++)if (a[ii][jj]==a[i][oface] && dp[ii][jj]>best){best=dp[ii][jj];last[i][j]=ii;lastface[i][j]=jj;}dp[i][j]=best+1;if (dp[i][j]>ans) ans=dp[i][j],ansi=i,ansf=j;}}printf("Case #%d\n",T);printf("%d\n",ans);print(ansi,ansf);}
}