24h購物| | PChome| 登入
2014-02-17 21:40:40| 人氣1,749| 回應0 | 上一篇 | 下一篇

[UVA][遞迴] 10795 - A Different Task

推薦 0 收藏 0 轉貼0 訂閱站台


  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 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.

epsfbox{p10795b.eps}

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$ le$N$ le$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$ le$i$ le$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

請參照

[ZJ][遞迴] d734. 另类Hanoi


#include <stdio.h>
int init[10005], goal[10005], n;
long long mod2[10005] = {1LL};
long long ret = 0;
int labelprefix, label;
void hanoiArbitrarily(int n, int goal) {
    if(n == 0)
        return;   
    if(labelprefix >= n) {
        init[n] = label;
        if(goal != label)
            ret += (1LL<<(n))-1;
        return;
    }
    if(init[n] != goal) {        
        int target = 6;
        target -= init[n];
        target -= goal;
        hanoiArbitrarily(n-1, target);// move other into buffer.
        init[n] = goal;//move n-th disk to goal.
        ret++;
        ret += (1LL<<(n-1))-1;
    } else {
        hanoiArbitrarily(n-1, goal);
    }
}
void hanoi(int n) {
    if(n == 0)
        return;
    if(labelprefix >= n)
        init[n] = label;
    if(init[n] != goal[n]) {
        int target = 6;
        target -= init[n];
        target -= goal[n];
        hanoiArbitrarily(n-1, target);
        labelprefix = n-1, label = target;
        ret++;
        init[n] = goal[n];//move n-th disk to goal.
    }
    hanoi(n-1);
}
int main() {
    int i, cases = 0;
    while(scanf("%d", &n) == 1 && n) {
        labelprefix = label = 0;
        for(i = 1; i <= n; i++)
            scanf("%d", &init[i]);
        for(i = 1; i <= n; i++)
            scanf("%d", &goal[i]);
        ret = 0;
        hanoi(n);
        printf("Case %d: %lld\n", ++cases, ret);
    }
    return 0;
}

台長: Morris
人氣(1,749) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][排列組合] 10634 - Say NO to Memorization
此分類上一篇:[UVA] 11880 - Ball in a Rectangle

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文