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 ( 1N60), 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 12 or 3. If the i-th ( 1iN) 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

 题目大意:标准的汉诺塔上有n个大小各异的盘子。给定一个初始局面,求它到达给定目标局面至少需要多少步。

 输入格式:第一行为正整数n,第二行包含n个1-3的整数,即初始局面中每个盒子锁在的柱子编号;第三行和第二行格式相同,为目标局面。

 分析:考虑编号最大的盘子。如果这个盘子在初始局面和目标局面中位于同一根柱子上,那么根本不需要移动它,而如果移动了,反而不可能得到最优解。这样,我们可以在初始局面和目标局面中,找出所在柱子不同的盘子中编号最大的一个,设为k,那么k必须移动。

  让我们设想一下,移动k之前的一瞬间,柱子上的情况。假设盘子k需要从柱子1移动到柱子2.由于编号比k大的盘子不需要移动,而且也不会碍事,所以我们直接把他们看成不存在;编号比k小的盘子既不能在柱子1上,也不能在柱子2上,因此只能在柱子3上。换句话说,这时柱子1只有盘子k,柱子2为空,柱子3从上到下一次是盘子1,2,3,...k-1。我们把这个局面称为参考局面。

  由于盘子的移动是可逆的,根据对称性,我们只需要求出初始局面和目标局面移动成参考局面的步数之和,然后加1(移动盘子k)即可。换句话说,我们需要写一个函数f(P,i,final),表示已知各盘子的初始柱子编号数组为P(具体来说,P[i] 代表盘子 i 的柱子编号),把盘子1,2,3,...,i 全部移到柱子final所需的步数,则本题的答案就是  f(start,k-1,6-start[k]-finish[k])+f(finish,k-1,6-start[k]-finish[k])+1。其中start[i]和finish[i]是本题输入中盘子i的初始柱子和目标柱子,k是上面所说的“必须移动的编号最大的盘子”的编号。我们把柱子编号为1,2,3,所以“除了柱子x和柱子y之外的那个柱子”编号为6-x-y。

  如何计算f(P,i,final)?假设P[i]=final,那么f(P,i,final)=f(P,i-1,final);否则需要先把前i-1个盘子挪到6-P[i]-final这个盘子做中转,然后把盘子i移动到柱子final,最后吧前i-1个盘子从中转的柱子移动到目标柱子final。注意,最后一个步骤是把i-1个盘子从一个柱子整体移动到另一个柱子,根据汉诺塔问题的经典结论,这个步骤需要2i-1-1步,加上移动盘子 i 的那一步,一共需要2i-1步。换句话说,当P[i]不等于final的时候f(P,i,final)=f(P,i-1,6-P[i]-final)+2i-1

 代码如下:

 #include<cstdio>

 long long f(int* P, int i, int final) {
if(i == ) return ;
if(P[i] == final) return f(P, i-, final);
return f(P, i-, -P[i]-final) + (1LL << (i-));
} const int maxn = + ;
int n, start[maxn], finish[maxn]; int main() {
int kase = ;
while(scanf("%d", &n) == && n) {
for(int i = ; i <= n; i++) scanf("%d", &start[i]);
for(int i = ; i <= n; i++) scanf("%d", &finish[i]);
int k = n;
while(k >= && start[k] == finish[k]) k--; long long ans = ;
if(k >= ) {
int other = -start[k]-finish[k];
ans = f(start, k-, other) + f(finish, k-, other) + ;
}
printf("Case %d: %lld\n", ++kase, ans);
}
return ;
}

最新文章

  1. Centos 下 mysql root 密码重置
  2. log4j.properties 配置的学习整理
  3. linux下mysql函数的详细案列
  4. 【堆】【kd-tree】bzoj2626 JZPFAR
  5. Linux runlevel 运行级别
  6. sql compact 使用EF无法更新的问题?
  7. Codeforces Round #342 (Div. 2) B. War of the Corporations 贪心
  8. windows下安装swoole。
  9. 阿里云至 Windows Azure 的 Linux 虚拟机迁移
  10. zepto源码学习-02 工具方法-详细解读
  11. hadoop集群之HDFS和YARN启动和停止命令
  12. linux环境下java读取sh脚本并执行
  13. solr(六): 集群
  14. 微软Office Online服务安装部署(一)
  15. 三维数组—— 与宝玉QQ群交流 之三
  16. MongoDB探索之路(三)——索引
  17. NLP总览
  18. cocos2d-x安装教程
  19. 《Android Studio有用指南》4.27 使用演示模式
  20. OC基础数据类型-NSArray

热门文章

  1. jqGrid 设置列宽
  2. Hadoop InputFormat浅析
  3. python seq
  4. 一切皆WEB
  5. jQuery中get与eq的区别
  6. weblogic目录结构
  7. SGU107——987654321 problem
  8. zookeeper-3.4.8 集群搭建
  9. ABAP多表关联查询
  10. eclipse加入git工具