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

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1736

The ( 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 (1 ≤
N ≤ 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

题目大意:  给n个磁盘的初始状态和末状态,求从初状态移动到末状态需要的最少步数;

因为每次只能移动一个磁盘并且,磁盘是按大小排列的,大的一定在小的下面,所以一定是从第n个开始移动;

如果从第一个开始移动的话,每次移动下面的都需要重新移动前几个,必然会多很多步骤;

先从第n个开始递减找初状态和末状态不同的那个磁盘k,如果k>=1,证明初状态和末状态不完全相同,否则,移动步数为0;

磁盘k以后的磁盘不需要移动,可以把他们看成不存在,不影响最终结果;

要把磁盘k从柱子a移动到指定柱子b,那么就需要把前k-1个磁盘移动到柱子c上按顺序排列;

把磁盘k在柱子a上,柱子b为空,柱子c上前k-1个磁盘按顺序排列的状态看作中间状态;

全过程就是从初状态到中间状态再到末状态,因为移动过程是可逆的,可以看做初状态和末状态都变成中间状态的步骤数在加一,一就是磁盘k从a柱子移动到b柱子的这个步骤;

函数f(p,k,c)就是计算p数组的前k个磁盘移动到c柱子的操作数;

可知总操作数=f(a,k-1,6-a[k]-b[k]) + f(b,k-1,6-a[k]-b[k]) + 1;

(a数组是初状态的磁盘位置,b数组是末状态的磁盘位置,a[i]是第i个磁盘刚开始所在的柱子数)

如果p[k] == c,那么f(p,k,c)=f(p,k-1,c);

否则就需要移动前k-1个磁盘到柱子6-c-p[k]上,在将第k个磁盘移动过去,在将前k-1个移过去;

根据汉诺塔的规律可知,将(k-1)个磁盘从一个柱子移动到另一个柱子上需要(2^(k-1))-1步,这里加上移动磁盘k的那一步,

即f(p,k,c) = f(p,k-1,6-c-p[k]) + 2^(k-1);

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;

ll f(int *p,int i,int c){
    if(i == 0) return 0;
    if(p[i] == c) return f(p,i-1,c);
    return f(p,i-1,6-p[i]-c) + (1LL << (i-1));
}

int main()
{
    int a[70],b[70],n;
    int s = 1;
    while(cin >> n && n){
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        int i;
        for(i = 1;i <= n;i++) cin >> a[i];//初状态
        for(i = 1;i <= n;i++) cin >> b[i];//末状态
        int k = n;
        ll ans = 0;
        for(;k>=1;k--)
            if(a[k]!=b[k])
                break;//找不一样的磁盘k
        if(k >= 1){
            int c = 6 - a[k] - b[k];
            ans = f(a,k-1,c) + f(b,k-1,c) + 1;
        }
        printf("Case %d: ",s++);
        cout << ans << endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值