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 ( 1N
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& ...
随机推荐
- libvirt(virsh命令介绍)
有了virt-install是安装虚拟机的命令,当然也需要一个管理虚拟机的命令了,那就是virsh. virsh命令使用 virsh <command> <domain-id> ...
- Android编程心得-设计一个可重用的自定义Dialog
我们在实际开发过程中,会遇到一个问题,我们的Dialog如果使用一般的方法进行设置调用出来,会有很多的重复代码,如何将Dialog按照自己的思路设计呢,并让其可重用呢,下面我来介绍一下 ...
- 2014 International Conference on Robotics and Computer Vision (ICRVC 2014)
2014机器人与计算机视觉国际会议ICRVC 与会地点:北京 与会时间:2014.10.24-26 截稿日期:2014-07-10 关于征稿: 语言:英文 主题: • Evolutionary Rob ...
- Android实现 再按一次退出 的三种方法 durationTime、timerTask 和Handler
目前很多Android应用都会实现按返回键时提示“再按一次推退出” 在这篇文章中总结了各家的方法,一般都是监听Activity的onKeyDown 或者onBackPressed方法 方法一: 直接计 ...
- 目录 of 2013-2014-1(内容已更新结束)
(内容已更新结束) UML部分: ---------------1.概述2.用例图3.类图4.顺序图 MVC部分: ----------------1.概述2.路由3.控制器4.视图5.模型6.安装部 ...
- Mac编程的官方文档(类似MSDN)
https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammin ...
- [置顶] ssize_t与size_t-linux
ssize_t: signed size_t [注释:signed 有符号] size_t: 标准C库中定义的,应为unsigned int [注释:unsigned 无符号] 一.size_t 增强 ...
- Python IDLE 运行错误:IDLE's subprocess didn't make connection. --已解决(原创)!
Python IDLE 错误描述: Subprocess Startup ErrorIDLE's subprocess didn't make connection. Either IDLE can' ...
- PS 滤镜算法原理 ——马赛克
% method : 利用邻域的随意一点取代当前邻域全部像素点 %%%% mosaic clc; clear all; addpath('E:\PhotoShop Algortihm\Image Pr ...
- 宽屏手机显示9.png的图片拉伸不均衡
制作的一个.9的背景图片,在一般的480宽的手机上显示没有问题,正常拉伸,用三星的一个宽屏手机测试时,没有完全拉伸,一边拉伸多一点,一边拉伸少一点 决绝办法:就是在制作.9的时候,我在横向拉伸的地方, ...