Uva10795 - A Different Task

原创 2015年07月09日 21:38:55

The (Three peg) Tower of Hanoi problem is a popular one in computer science. Brie
y 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 nd the sequence of moves for
smaller number of disks such as 3 or 4. A trivial computer program can nd the case of large number
of disks also.
Here we have made your task little bit difficult by making the problem more
exible. 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 nd out the
minimum number of moves, which will transform the rst 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 le 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 speci ed 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

#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXN = 65;

long long f(int p[],int i,int final)
{
    if(i==0)return 0;
    if(p[i]==final) return f(p,i-1,final);
    return f(p,i-1,6-final-p[i]) + ((long long)1<<(i-1));
}
int main()
{
    int n, cs=1, i;
    int start[MAXN],final[MAXN];
     
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)break;
        for(i=1; i<=n; i++) scanf("%d",&start[i]);
        for(i=1; i<=n; i++) scanf("%d",&final[i]);
        int k = n;
        while(k>0&&final[k]==start[k])k--;
        long long ans = 0;
        if(k>0)
        {
            ans = f(start,k-1,6-final[k]-start[k]) + f(final,k-1,6-final[k]-start[k]) + 1;
        }
        printf("Case %d: %lld\n",cs++,ans);
    }
}


 

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

Uva 10795 - A Different Task

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem...

Uva 10795 - A Different Task 【模拟】

A Different Task \epsfbox{p10795a.eps} The (Three peg) Tower of Hanoi problem is a popular one in...

Uva 10795 A Different Task 解题报告(递归+思维)

A Different Task  The (Three peg) Tower of Hanoi problem is a popular one in computer...
  • kbdwo
  • kbdwo
  • 2014年07月15日 10:53
  • 667

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

 题目大意:就是普通的汉诺塔问题,给出n,表示说有n个大小不同的碟子,然后再给出每个碟子的初始位置和目标位置,要求计算出最少的步数使得每个碟子都移动到它的目标位置。 思路:考虑编号最大的盘子,...

uva10795 A Different Task

构造+递归
  • sdfzyhx
  • sdfzyhx
  • 2016年11月10日 16:43
  • 105

uva 10795 新汉诺塔问题

题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem...
  • lyqzzy
  • lyqzzy
  • 2017年01月31日 06:55
  • 109

uva10795(汉莫塔)

题意: 给出碟子的数量, 然后给出一开始碟子的位置,和最终碟子的位置,问最少移动几次可以完成,要求和普通汉诺塔一样,大的不能叠在小的上面; 思路: 感觉没看题解完全想不出来这个思路; ...

例题1.11 新汉诺塔问题 UVa10795

1.题目描述:点击打开链接 2.解题思路:本题是经典汉诺塔问题的变形题,根据解汉诺塔问题时的思路,我们试着找递推公式来解决本题。考虑最大的盘子,设它为k,如果此时它不在目标柱子上,说明它必须移动。现在...

UVa10795

Problem: A Different Task Description: 给出汉诺塔的一种形态,问你至少要通过多少次的移动才能到达给定的另一种形态。 Solution: 这个题目和HDU426...

uva 10795(汉诺塔)

题意:汉诺塔问题,有n个盘子大小不同,编号大的更大,有三个柱子可以套盘子,盘子必须小的在大的上面,每次只能移动一个盘子,给出初始状态和目标状态,问最少几步可以从初始状态到目标状态。 题解:如果盘...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Uva10795 - A Different Task
举报原因:
原因补充:

(最多只允许输入30个字)