本题关键是得到一个参照状态
首先我们要求出一个最大的k,这个编号为k的圆盘初始位置与结束位置不同
显然,当k=0的时候答案为0,因为编号从1开始此时已经不用移动
当k>1的时候,我们可以这样思考
st 起始
ed 目标
tmp 缓冲
我们要把 第k个盘子从st移动到ed , 那么其他k-1个盘子必须在tmp上面 用f(start,k-1,6-st[k]-ed[k])表示k-1个圆盘都移动到另外一个柱子上的耗费
则我们最后的答案是
ans=f(start,k-1,6-st[k]-ed[k])+f(finsh,k-1,6-st[k]-ed[k])+1
这里注意f(finsh,k-1,6-st[k]-ed[k])的意思是目标态移动到参考状态的步数,1表示k移动的耗费
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description

A Different Task
A Different Task |

The (Three peg) Tower of Hanoi problem is a popular one in computer science. Briefly the problem is to transfer all the disks from peg-A to peg-C using peg-B as intermediate one in such a way that at no stage a larger disk is above a smaller disk. Normally, we want the minimum number of moves required for this task. The problem is used as an ideal example for learning recursion. It is so well studied that one can find the sequence of moves for smaller number of disks such as 3 or 4. A trivial computer program can find the case of large number of disks also.
Here we have made your task little bit difficult by making the problem more flexible. Here the disks can be in any peg initially.

If more than one disk is in a certain peg, then they will be in a valid arrangement (larger disk will not be on smaller ones). We will give you two such arrangements of disks. You will have to find out the minimum number of moves, which will transform the first arrangement into the second one. Of course you always have to maintain the constraint that smaller disks must be upon the larger ones.
Input
The input file contains at most 100 test cases. Each test case starts with a positive integer N ( 1N
60), which means the number of disks. You will be given the arrangements in next two lines. Each arrangement will be represented by N integers, which are 1, 2 or 3. If the i-th ( 1
i
N) integer is 1, you should consider that i-th disk is on Peg-A. Input is terminated by N = 0. This case should not be processed.
Output
Output of each test case should consist of a line starting with `Case #: ' where # is the test case number. It should be followed by the minimum number of moves as specified in the problem statement.
Sample Input
3 1 1 1 2 2 2 3 1 2 3 3 2 1 4 1 1 1 1 1 1 1 1 0
Sample Output
Case 1: 7 Case 2: 3 Case 3: 0
Problem setter: Md. Kamruzzaman
Special Thanks: Derek Kisman (Alternate Solution), Shahriar Manzoor (Picture Drawing)
Miguel Revilla 2004-12-10
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n;
int st[110];
int ed[110];
int lst;
long long f(int *p,int cnt,int to){
if(cnt==0) return 0;
if(p[cnt]==to) return f(p,cnt-1,to);
return f(p,cnt-1,6-p[cnt]-to)+(1LL<<(cnt-1));
}
int main(){
int cs=1;
while(~scanf("%d",&n)){
if(!n) break;
for(int i=1;i<=n;i++)
scanf("%d",&st[i]);
for(int i=1;i<=n;i++)
scanf("%d",&ed[i]);
lst=n;
while(lst>0&&st[lst]==ed[lst]) lst--;
if(lst==0)
printf("Case %d: 0\n",cs++);
else{
long long ret;
ret=f(st,lst-1,6-st[lst]-ed[lst]);
ret+=f(ed,lst-1,6-st[lst]-ed[lst]);
ret+=1;
printf("Case %d: %lld\n",cs++,ret);
}
}
return 0;
}