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 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 1,
2 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个大小不同的盘子,然后再给出每个盘子的初始位置和目标位置,要求计算出最少的步数使得每个盘子都移动到它的目标位置。
解题思路:找到需要移动的最大盘子i,然后1~i-1个盘子需要移动到6 - now[i] - end[i],同样的需要让i - 1移动到6 - now[i] - end[i],需要让1~i - 2都移动到 6 -
(6 - now[i] - end[i])- now[i - 1],所以很明显是递归求解,注意要将1~i - 2移动到i - 1上面。
然后进行完上述操作,从1~i-1都在同一个柱子上,可以从大到小将每个碟子还原到目标位置。
。
#include<stdio.h> #include<string.h> int n; long long begin[65], last[65], num[65]; long long move(int b, int f) { if (b < 0) return 0; if (begin[b] == f) { //当begin[b] == f时,则b盘子无需移动,其总移动书等于move(b - 1, f) return move(b - 1, f); } else return move(b - 1, 6 - begin[b] - f) + num[b]; //当begin[b] != f时,返回将b - 1个盘子移到中转柱子上的步数 加上 把盘子b移到f柱子上的 //步数(1) 加上 把b - 1个盘子从中转柱子移到f柱子的步数(2^b-1 - 1) } long long temp; long long getAns() { long long ans = 0; int i; for (i = n - 1; i >= 0; i--) { if (begin[i] != last[i]) { //找出需要移动的最大盘子 temp = 6 - begin[i] - last[i];//中转柱子 ans += move(i - 1, temp) + 1; //先把i - 1个盘子移到6 - begin[i] - last[i]做中转,然后移动盘子i到final柱子 i--; break; } } for (; i >= 0; i--) { if (last[i] != temp) { //若i - 1之后的盘子最终位置不在中转柱子上,将其移到目标柱子上,并修改中转柱子 temp = 6 - last[i] - temp; ans += num[i]; } } return ans; } int main() { int Case = 1; while (scanf("%d", &n) == 1, n) { memset(begin, 0, sizeof(begin)); memset(last, 0, sizeof(last)); memset(num, 0, sizeof(num)); for (int i = 0; i < n; i++) { scanf("%lld", &begin[i]); } for (int i = 0; i < n; i++) { scanf("%lld", &last[i]); } num[0] = 1; for (int i = 1; i < 60; i++) { num[i] = num[i - 1] * 2; } printf("Case %d: %lld\n", Case++, getAns()); } return 0; }