UVA 10795 - A Different Task(递归)
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 1, 2 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(递归)的更多相关文章
- UVA 10795 A Different Task(汉诺塔 递归))
A Different Task The (Three peg) Tower of Hanoi problem is a popular one in computer science. Briefl ...
- 【汉诺塔问题】UVa 10795 - A Different Task
[经典汉诺塔问题] 汉诺(Hanoi)塔问题:古代有一个梵塔,塔内有三个座A.B.C,A座上有64个盘子,盘子大小不等,大的在下,小的在上.有一个和尚想把这64个盘子从A座移到B座,但每次只能允许移动 ...
- UVa 10795 - A Different Task 对称, 中间状态, 数位DP 难度: 3
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVA 10795 A Different Task(模拟)
题目链接:https://vjudge.net/problem/UVA-10795 一道比较有思维含量的一道题: 注意一种分治的思想和“除了柱子x和柱子y之外的那个柱子”编号的问题. 首先在初始局面和 ...
- UVa 10795 - A Different Task
题目大意:给出n,表示说有n个大小不同的盘子,然后再给出每个盘子的初始位置和目标位置,要求计算出最少的步数使得每个盘子都移动到它的目标位置. 分析: 首先找最大不在目标柱子上的盘子K,因为如果最大的 ...
- 二分图最大匹配(匈牙利算法) UVA 670 The dog task
题目传送门 /* 题意:bob按照指定顺序行走,他的狗可以在他到达下一个点之前到一个景点并及时返回,问狗最多能走多少个景点 匈牙利算法:按照狗能否顺利到一个景点分为两个集合,套个模板 */ #incl ...
- UVa 699 The Falling Leaves(递归建树)
UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶 而且叶子只会垂直下落 每个节点保存的值为那个节点上的叶子数 求所有叶子全部下落后 地面从左到右每 ...
- UVa新汉诺塔问题(A Different Task,Uva 10795)
主要需要理递归函数计算 #define MAXN 60+10 #include<iostream> using namespace std; int n,k,S[MAXN],F[MAXN] ...
- UVA 10795 新汉诺塔问题
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- windows无效字符名导致的错误及解决办法
今天用file_put_content($fileName,$data)产生错误:内容如下: Warning: file_put_contents(images/7d5636992a7395f9174 ...
- 转:Javascript的10个设计缺陷
作者: 阮一峰 日期: 2011年6月30日 前几篇文章,我经常说Javascript的设计不够严谨,有很多失误. 今天的这一篇,前半部分就谈为什么会这样,后半部分将列举Javascript的10个设 ...
- JavaScript的实现
了解了JavaScript是干什么的< 对一些词的理解 >,下面该知道它是怎么实现的. 一个完整的JavaScript是由三部分组成的,如下图 ECMAScript 可以为不同种类的宿主环 ...
- C Primer Plus 读书笔记之C基础回顾
目标代码文件.可执行文件和库 C编程的基本策略是使用程序将源代码文件转换为可执行文件,此文件包含可以运行的机器语言代码.C分两步完成这一工作:编译和链接.编译器将源代码转换为中间代码,链接器将此中间代 ...
- BZOJ 4143: [AMPPZ2014]The Lawyer( sort )
水题... 排序搞出每天的会议有哪些, 然后再按照会议的开始时间和结束时间排序, 最晚开始的和最早结束的会议不是同一场而且最晚开始的时间>最早结束的会议就有可能方案 -------------- ...
- python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块
正则表达式 语法: mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...
- 09-IOSCore - 应用互动、UIImagePickerController
一.调用系统程序/服务 1. 应用互动数据权限 1) 跟自己本地数据交互 2) 跟网络服务器/云数据交互 3) 跟系统数据服务交互 4) 跟其它应用程序交互 2. 基础 URL 统一资源定位 一个特殊 ...
- extern、static、auto、register 定义变量的不同用法
首先得说明什么叫“编译单元”.每个 .c 文件会被编译为一个 .o 文件,这个就是一个编译单元.最后所有的编译单元被链接起来,就是一个库或一个程序. 一个变量/函数,只要是在全局声明的,链接之后都隐含 ...
- IntelliJ IDEA 开发swing(二)
原文:idea开发swing(二) 闲话少说,书接idea开发swing(一). 程序编译完成后,需要打包发布,如果有fat_jar的同学可以通过该插件打包,这里是使用ant来打包,步骤如下: 一.编 ...
- android apk打包之后js调用失效的解决办法
现在android下应用开发的界面用html5+css3写,交互用javascript和java沟通,但是用上混淆后发现javascript调用java类定义的方法老说找不到这个方法.一番折腾后发现是 ...