uva----(10794) 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 ( 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

代码:

 1 #include<cstdio>
 2 const int maxn =70;
 3 int n,start[maxn],finish[maxn];
 4 long long Func(int *p,int i,int final)
 5 {
 6     if(i==0) return 0;
 7     if(p[i]==final) return Func(p,i-1,final);
 8     return Func(p,i-1,6-p[i]-final)+(1LL<<(i-1));
 9 }
10 int main()
11 {
12     int kase=0;
13     while(scanf("%d",&n)==1&&n)
14     {
15       for(int i=1;i<=n;i++)
16             scanf("%d",&start[i]);
17       for(int i=1;i<=n;i++)
18           scanf("%d",&finish[i]);
19       int k=n;
20       while(k>=1 && start[k]==finish[k])k--;
21
22       long long ans=0;
23       if(k>=1)
24       {
25           int other=6-start[k]-finish[k];
26           ans =Func(start,k-1,other)+Func(finish,k-1,other)+1;
27       }
28       printf("Case %d: %lld\n",++kase,ans);
29     }
30 }


Problem setter: Md. Kamruzzaman
Special Thanks: Derek Kisman (Alternate Solution), Shahriar Manzoor (Picture Drawing)

Miguel Revilla 2004-12-10

uva----(10794) A Different Task,布布扣,bubuko.com

时间: 2024-10-28 05:29:16

uva----(10794) A Different Task的相关文章

二分图匹配(匈牙利算法) UVA 670 The dog task

题目传送门 1 /* 2 题意:bob按照指定顺序行走,他的狗可以在他到达下一个点之前到一个景点并及时返回,问狗最多能走多少个景点 3 匈牙利算法:按照狗能否顺利到一个景点分为两个集合,套个模板 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 #include <vector> 10 using namespace

uva 10795 - A Different Task (递归+状态转移)

题目链接:uva 10795 - A Different Task 思路来源于:点击打开链接 题意: 新汉若塔问题,有n个盘子,放在3个盘子上,给你一个初始状态和一个结束状态,问你最小步数怎样到达. 思路: 递归+状态转移,直接从初态到末态好像不是那么好办,对最大的一块n,首先肯定要把他放在末态的位置上,假设开始在1号位置,要放到3号位置,那么必须先到达这个状态s:1~n-1必须都从大到小放在2上面,然后放n,然后将1~n-1转移到末态,由对称性,也即可以变为末态转移到状态s,那么处理起来就可以

uva 10795 A Different Task(递归模拟)

uva 10795 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

Uva 10795 - A Different Task 【模拟】

A Different Task \epsfbox{p10795a.eps} 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

【汉诺塔问题】UVa 10795 - A Different Task

[经典汉诺塔问题] 汉诺(Hanoi)塔问题:古代有一个梵塔,塔内有三个座A.B.C,A座上有64个盘子,盘子大小不等,大的在下,小的在上.有一个和尚想把这64个盘子从A座移到B座,但每次只能允许移动一个盘子,并且在移动过程中,3个座上的盘子始终保持大盘在下,小盘在上.在移动过程中可以利用B座,要求打印移动的步骤.如果只有一个盘子,则不需要利用B座,直接将盘子从A移动到C. 如果有2个盘子,可以先将盘子1上的盘子2移动到B:将盘子1移动到c:将盘子2移动到c.这说明了:可以借助B将2个盘子从A移

UVa 10795 - A Different Task

题目大意:给出n,表示说有n个大小不同的盘子,然后再给出每个盘子的初始位置和目标位置,要求计算出最少的步数使得每个盘子都移动到它的目标位置. 分析:  首先找最大不在目标柱子上的盘子K,因为如果最大的盘子在目标柱子上它不需要移动,也不碍事. 因此问题就成了把K移动到目标柱子,把1到(k-1)移动到中转柱子,所以假设K从A移动到B,A只有K,B是空的,C上面是K-1到1,把这个局面称为参考局面.因为移动是对称的,所以从参考局面移到目标局面与目标局面移到参考局面是一样的步数. 所以问题变成答案=从初

UVA 10795 A Different Task(新汉诺塔问题)

 题目大意:就是普通的汉诺塔问题,给出n,表示说有n个大小不同的碟子,然后再给出每个碟子的初始位置和目标位置,要求计算出最少的步数使得每个碟子都移动到它的目标位置. 思路:考虑编号最大的盘子,如果它在初始位置和目标局面在同一根柱子上,那么我们不需要移动它. 由于盘子的移动是可逆的,根据对称性,我们只需要求出初始局面和目标局面移动形成的参考局面的步数之和,然后加一即可. 我们可以写一个函数f(P, i, final)表示已知各盘子初始编号为P,把1,2,3....i移动到final柱子上所需要

贪心思维 专题记录 2017-7-21

A.UVa 10382 - Watering Grass 题目大意: 有一块草坪,长为l,宽为w,在它的水平中心线上有n个位置可以安装喷水装置,各个位置上的喷水装置的覆盖范围为以它们自己的半径ri为圆.求出最少需要的喷水装置个数. 思路 :转化一下 将二维降成一维      d = sqrt(1.0*r*r-w*w/4.0) 接着就是区间覆盖问题了 #include <bits/stdc++.h> using namespace std; const int maxn = 10000+10;

思维专题(不定期更新)

1.UVa 11100 - The Trip, 2007 题意:给出若干大小不同的包裹,小的能够装在大的包裹里面.求最小的大包裹数,并且保证在所有的大包裹中,所含有的小包裹数目最小. 思路:显然,相同大小的包只能放在不同的大包里,那么最小的大包数目就是相同大小的包的最大数目,记为k.之后,根据从小到大排序后,对于每个大包i可选取从i开始,间隔k个包的那些包裹作为该大包从里到外的各个包裹. 1 #include<iostream> 2 #include<algorithm> 3 us

uva 11728 - Alternate Task(数论)

题目链接:uva 11728 - Alternate Task 题目大意:给出S,求N,要求N所有的因子和为S. 解题思路:枚举因子i,所有整除i的数和加上i. #include <cstdio> #include <cstring> const int N = 1005; int n, c[N], v[N]; void init () { memset(c, 0, sizeof(c)); memset(v, -1, sizeof(v)); for (int i = 1; i &l