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);
    }
}


 

 

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

uva10795 A Different Task

题意思路都来自大白书上,用到参考局面的思想非常巧妙 #include #include #include #include #include #include #include ...
  • qq_21057881
  • qq_21057881
  • 2016年03月16日 20:02
  • 302

UVa10795 - A Different Task(递归)

题目链接简介:新汉诺塔问题(给出了初始状态和目标状态)分析: 我们考虑最大的盘子,如果这个盘子初始状态和中止状态在一个柱子上,说明我们根本不用移动ta 那么我们找到编号最大的需要移动是盘子k(初始...
  • wu_tongtong
  • wu_tongtong
  • 2017年10月15日 10:28
  • 74

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

这个题不同的是开始状态不规则,目标状态也不规则 我们这里有个折中的方法,就是从开始和目标两个状态同时向一个参考状态移动。 我们分析这个问题,会发现我们必须先把最大的圆盘放到目标位置,所以目前位置s...
  • shankeliupo
  • shankeliupo
  • 2013年04月22日 20:51
  • 641

uva10795 - A Different Task 汉诺塔 给出状态问步数

A Different Task  The (Three peg) Tower of Hanoi problem is a popular one in computer scienc...
  • corncsd
  • corncsd
  • 2014年01月29日 19:22
  • 380

poj-1850-数学

题意: 给你一串字母,求他们应该是第几位。 a - 1  b - 2  ...  z - 26  ab - 27  ...  az - 51  bc - 52  ...  vwx...
  • rowanhaoa
  • rowanhaoa
  • 2013年01月21日 15:47
  • 498

poj1850 code 字串的编号

题意:给定字符串编号规则,表中字串满足字符从左到右严格递增,
  • smilewsw
  • smilewsw
  • 2014年08月11日 17:17
  • 382

POJ 1850 Code(组合数学)

Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8662   Accepted:...
  • yeguxin
  • yeguxin
  • 2015年07月22日 16:40
  • 640

poj1850

可以算组合吗?长度为2的字母,一共有C(26,2)个,这很难理解么?然后推一下当前要算的字母在哪个位置即可了。 #include #include "string" using namespac...
  • zhengnanlee
  • zhengnanlee
  • 2014年01月17日 18:41
  • 1591

Poj1850

 Poj1850
  • Legendary_
  • Legendary_
  • 2014年11月17日 11:09
  • 304

POJ 题目1850 Code(组合数学)

Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8404   Accepted:...
  • yu_ch_sh
  • yu_ch_sh
  • 2015年01月11日 18:20
  • 395
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Uva10795 - A Different Task
举报原因:
原因补充:

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