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 ( 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 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& ...
随机推荐
- 数据库中操作XML(openXML)
最近公司项目需要在数据库中操作XML,因此系统的学习了一下 一.openxml的格式 OPENXML( idoc int [ in] , XPathnvarchar [ in ] , [ flags ...
- Codeforces Round #198 (Div. 2) C. Tourist Problem
C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Steve Yegge:Google面试秘籍
我憋了很长时间想写点关于去Google面试的秘籍.不过我总是推迟,因为写出来的东西会让你抓狂.很可能是这样.如果按统计规律来定义"你"的话,这文章很可能让你不爽. 为啥呢?因为啊- ...
- Windows Phone 8初学者开发—第20部分:录制Wav音频文件
原文 Windows Phone 8初学者开发—第20部分:录制Wav音频文件 原文地址:http://channel9.msdn.com/Series/Windows-Phone-8-Develop ...
- 初探 FFT/DFT
有用的学习链接&书籍 傅立叶变化-维基百科 离散傅立叶变化-维基百科·长整数与多项式乘法 维基百科看英文的更多内容&有趣的图 快速傅立叶变化-百度百科,注意其中的图! 组合数学(第4版 ...
- HTTP的请求头标签If-Modified-Since
一直以来没有留意过HTTP请求头的IMS(If-Modified-Since)标签. 最近在分析Squid的access.log日志文件时,发现了一个现象. 就是即使是对同一个文件进行HTTP请求,第 ...
- JS中setTimeout()的使用方法具体解释
1. SetTimeOut() 1.1 SetTimeOut()语法样例 1.2 用SetTimeOut()运行Function ...
- 第十一章 认识与学习BASH
系统支持的shell在 /etc/shells里面 Bash Shell 的功能: 1.命令修补能力(histroy) 2.命令与档案补全功能 3.命令别名设定功能 4.工作前景背景控制 5.支持 ...
- Service的开启和停止以及生命周期
1.清单文件 <service android:name=".TestService"></service> 2.开启Service Intent inte ...
- C++和JNI的数据转换
链接地址:http://blog.csdn.net/manymore13/article/details/19078713 转载地址:http://www.cnblogs.com/daniel-she ...