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

题意:给定一个汉若塔初始和目标,求最少步数。

思路:每次先移动大的,然后其他的肯定要先全部堆到没用的柱子上,然后最后在一个个去放回位置

代码:

#include <stdio.h>
#include <string.h> const int N = 65;
typedef long long LL; int n, start[N], end[N];
LL mi[N], ans, t; void init() {
mi[0] = 0;
for (int i = 1; i <= 60; i ++)
mi[i] = mi[i - 1] * 2 + 1;
} LL solve(int i, int pos) {
if (i == 0)
return 0;
if (pos == start[i])
return solve(i - 1, pos);
else
return solve(i - 1, 6 - pos - start[i]) + 1 + mi[i - 1];
} int main() {
init();
int cas = 0;
while (~scanf("%d", &n) && n) {
ans = 0; int i;
for (i = 1; i <= n; i ++)
scanf("%d", &start[i]);
for (i = 1; i <= n; i ++)
scanf("%d", &end[i]);
for (i = n; i >= 1; i --) {
if (end[i] != start[i]) {
ans = solve(i - 1, 6 - start[i] - end[i]) + 1;
t = 6 - start[i] - end[i];
break;
}
}
for (int j = i - 1; j >= 1; j --) {
if (end[j] == t) continue;
ans += mi[j - 1] + 1;
t = 6 - t - end[j];
}
printf("Case %d: %lld\n", ++cas, ans);
}
return 0;
}

UVA 10795 - A Different Task(递归)的更多相关文章

  1. UVA 10795 A Different Task(汉诺塔 递归))

    A Different Task The (Three peg) Tower of Hanoi problem is a popular one in computer science. Briefl ...

  2. 【汉诺塔问题】UVa 10795 - A Different Task

    [经典汉诺塔问题] 汉诺(Hanoi)塔问题:古代有一个梵塔,塔内有三个座A.B.C,A座上有64个盘子,盘子大小不等,大的在下,小的在上.有一个和尚想把这64个盘子从A座移到B座,但每次只能允许移动 ...

  3. UVa 10795 - A Different Task 对称, 中间状态, 数位DP 难度: 3

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  4. UVA 10795 A Different Task(模拟)

    题目链接:https://vjudge.net/problem/UVA-10795 一道比较有思维含量的一道题: 注意一种分治的思想和“除了柱子x和柱子y之外的那个柱子”编号的问题. 首先在初始局面和 ...

  5. UVa 10795 - A Different Task

    题目大意:给出n,表示说有n个大小不同的盘子,然后再给出每个盘子的初始位置和目标位置,要求计算出最少的步数使得每个盘子都移动到它的目标位置. 分析:  首先找最大不在目标柱子上的盘子K,因为如果最大的 ...

  6. 二分图最大匹配(匈牙利算法) UVA 670 The dog task

    题目传送门 /* 题意:bob按照指定顺序行走,他的狗可以在他到达下一个点之前到一个景点并及时返回,问狗最多能走多少个景点 匈牙利算法:按照狗能否顺利到一个景点分为两个集合,套个模板 */ #incl ...

  7. UVa 699 The Falling Leaves(递归建树)

    UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶  而且叶子只会垂直下落   每个节点保存的值为那个节点上的叶子数   求所有叶子全部下落后   地面从左到右每 ...

  8. UVa新汉诺塔问题(A Different Task,Uva 10795)

    主要需要理递归函数计算 #define MAXN 60+10 #include<iostream> using namespace std; int n,k,S[MAXN],F[MAXN] ...

  9. UVA 10795 新汉诺塔问题

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

随机推荐

  1. poj 2356鸽笼原理水题

    关于鸽笼原理的知识看我写的另一篇博客 http://blog.csdn.net/u011026968/article/details/11564841 (需要说明的是,我写的代码在有答案时就输出结果了 ...

  2. 基于visual Studio2013解决C语言竞赛题之0510求最大和

     题目

  3. NanShan即时通讯论——HTML5的优势与劣势

    原文:NanShan即时通讯论--HTML5的优势与劣势 NanShan即时通讯 学习html时才是XHTML 1.0,接着是 HTML4.01,再到HTML5,如今HTML5 開始吸引越来越多的人的 ...

  4. oracle expdp和impdp使用例子

    情景: 由于生产需求,需要把rmtel用户的数据完全复制一份给rmtel_xzy,但排除rmtel用户 ('CAB_JJXPORT_TAB','T_SERVICEXX','TB_CROSSCONNEC ...

  5. 【Java Web】使用URLRewrite实现网站伪静态

    大部分搜索引擎都会优先考虑收录静态的HTML页面,而不是动态的*.jsp.*.php页面.但实际上绝大部分网站都是动态的,不可能全部是静态的HTML页面,因此互联网上大部分网站都会考虑伪静态——就是将 ...

  6. 基于visual Studio2013解决C语言竞赛题之0802图书信息查询

     题目 解决代码及点评 /* 功能:有一批图书,每本书有:书名(name),作者(author) , 编号(num),出版日期(date)四个数据, 希望输入后按书名的字母顺序将各书的记录排列好, ...

  7. C语言数据结构----栈的定义及实现

    本节主要说的是数据结构中的栈的基本定义和实现的方式,其中实现的方式采用的是复用顺序表和单向链表的方式. 一.栈的基本定义 1.栈是一种特殊的线性表,只能从固定的方向进出,而且栈进出的基本原则是:先进栈 ...

  8. while和do while习题

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 练习 { ...

  9. linux登录windows服务器

    在公司同时也兼顾了王老师会议网站的任务,我喜欢用linux,而会议网站托管在windows系统上,虽然装了双系统,但我还是比较懒,不喜欢经常切换系统.还好,linux可以实现登录windows服务器. ...

  10. Cocos2d-x游戏的场景结构布局