uva 10051 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:
• 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
(1 ≤ N ≤ 500) indicating the number of cubes you are given. The ith (1 ≤ i ≤ N) of the next N lines
contains the description of the ith 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
whitespace 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 front
Case #2
8
1 bottom
2 back
3 right
4 left
6 top
8 front
9 front
10 top
题目大意:
按从轻到重给你n个立方块,立方块的6个面上有颜色。让你从重到轻搭一个塔,第i个塔的顶面要和第i+1的底面颜色相等。

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
const int N=501;
int dp[N][6];
int cube[N][6];
string mark[6]={"front","back","left","right","top","bottom"};
int inde[N][N][2];
void path(int x,int y,int hei)
{
    if(hei==0)
    return ;
    cout<<x<<" "<<mark[y]<<endl;
    path(inde[x][y][0],inde[x][y][1],hei-1);
}
int n,ans;int main()
{
    ios::sync_with_stdio(false);
    int lastFace,lastIndex,posx,posy,t=1;
    int flag=0;
    while(cin>>n,n)
    {
        memset(dp,0,sizeof(dp));
        posx=posy=ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<6;j++)
            cin>>cube[i][j];
        }
        for(int i=0;i<6;i++)
        dp[n][i]=1;
        for(int i=n-1;i>=1;i--)
        {
            for(int j=0;j<6;j++)
            {
                dp[i][j]=1;
                for(int k=i+1;k<=n;k++)
                {
                    for(int m=0;m<6;m++)
                    {
                        if(cube[i][(j+1)%2?j+1:j-1]==cube[k][m]&&dp[i][j]<dp[k][m]+1)
                        {
                            dp[i][j]=dp[k][m]+1;
                            lastIndex=k;
                            lastFace=m;
                        }
                    }
                }
                inde[i][j][0]=lastIndex;
                inde[i][j][1]=lastFace;
                if(ans<dp[i][j])
                {
                    ans=dp[i][j];
                    posx=i;
                    posy=j;
                }
            }
        }
        if(flag++)
        cout<<endl;
        cout<<"Case #"<<t++<<endl;
        cout<<ans<<endl;
        path(posx,posy,ans);
    }
    return 0;
}




题解:
转移方程很好想,因为和lis问题异曲同工。打印路径的方法蒙了,用搜索,写到半路突然想到如果用搜索找路径还用dp求解干毛? 想不动了,看别人的题解知道了打印路径的过程可以用一个二维或者三维数组记录当前状态的上一个状态的位置,然后递归的找结果状态的上一个位置就能找到路径了~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值