uva----(10794) 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<cstdio>
const int maxn =;
int n,start[maxn],finish[maxn];
long long Func(int *p,int i,int final)
{
if(i==) return ;
if(p[i]==final) return Func(p,i-,final);
return Func(p,i-,-p[i]-final)+(1LL<<(i-));
}
int main()
{
int kase=;
while(scanf("%d",&n)==&&n)
{
for(int i=;i<=n;i++)
scanf("%d",&start[i]);
for(int i=;i<=n;i++)
scanf("%d",&finish[i]);
int k=n;
while(k>= && start[k]==finish[k])k--; long long ans=;
if(k>=)
{
int other=-start[k]-finish[k];
ans =Func(start,k-,other)+Func(finish,k-,other)+;
}
printf("Case %d: %lld\n",++kase,ans);
}
}
Problem setter: Md. Kamruzzaman
Special Thanks: Derek Kisman (Alternate Solution), Shahriar Manzoor (Picture Drawing)
Miguel Revilla 2004-12-10
uva----(10794) A Different Task的更多相关文章
- 二分图最大匹配(匈牙利算法) UVA 670 The dog task
题目传送门 /* 题意:bob按照指定顺序行走,他的狗可以在他到达下一个点之前到一个景点并及时返回,问狗最多能走多少个景点 匈牙利算法:按照狗能否顺利到一个景点分为两个集合,套个模板 */ #incl ...
- 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(递归)
A Different Task The (Three peg) Tower of Hanoi problem is a popular one in computer science. Brie ...
- 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
题目大意:给出n,表示说有n个大小不同的盘子,然后再给出每个盘子的初始位置和目标位置,要求计算出最少的步数使得每个盘子都移动到它的目标位置. 分析: 首先找最大不在目标柱子上的盘子K,因为如果最大的 ...
- 【汉诺塔问题】UVa 10795 - A Different Task
[经典汉诺塔问题] 汉诺(Hanoi)塔问题:古代有一个梵塔,塔内有三个座A.B.C,A座上有64个盘子,盘子大小不等,大的在下,小的在上.有一个和尚想把这64个盘子从A座移到B座,但每次只能允许移动 ...
- UVA 10795 A Different Task(模拟)
题目链接:https://vjudge.net/problem/UVA-10795 一道比较有思维含量的一道题: 注意一种分治的思想和“除了柱子x和柱子y之外的那个柱子”编号的问题. 首先在初始局面和 ...
- uva 11728 - Alternate Task(数论)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/36409469 option=com_onli ...
- UVA 11728 - Alternate Task (数论)
Uva 11728 - Alternate Task 题目链接 题意:给定一个因子和.求出相应是哪个数字 思路:数字不可能大于因子和,对于每一个数字去算出因子和,然后记录下来就可以 代码: #incl ...
- uva 11728 Alternate Task
vjudge 上题目链接:uva 11728 其实是个数论水题,直接打表就行: #include<cstdio> #include<algorithm> using names ...
随机推荐
- [SAP ABAP开发技术总结]Form(subroutine)、Function参数传值传址
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- [JAVA设计模式]第一部分:接口、抽象类、设计原则
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- CUBRID学习笔记 30 复制表结构 cubrid教程
语法 CREATE {TABLE | CLASS} <new_table_name> LIKE <old_table_name> 如下 CREATE TABLE a_tbl( ...
- 本地设置正常,放服务器上就报 System.Security系统找不到指定的文件解决方法
在应用程序池设置中将“加载用户配置文件”(Load User Profile)设置为true,问题就解决.
- bzoj 2957: 楼房重建 线段树
2957: 楼房重建 Time Limit: 10 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 小A的楼房外有一大片施 ...
- AS3 求两条直线的交点
//粘贴到帧上运行即可 var p1Start:Point = new Point(0,0); var p1End:Point = new Point(50,50); var p2Start:Poin ...
- 转-Android Studio *.jar 与 *.aar 的生成与*.aar导入项目方法
主要讲解Android Studio中生成aar文件以及本地方式使用aar文件的方法. 在Android Studio中对一个自己库进行生成操作时将会同时生成*.jar与*.aar文件. 分别存储位置 ...
- iOS - Swift 异常处理
前言 在 Swift 1.0 时代是没有异常处理和抛出机制的,如果要处理异常,要么使用 if else 语句或 switch 语句判断处理,要么使用闭包形式的回调函数处理,再要么就使用 NSError ...
- 2016中国APP分类排行榜参选入围产品公示
2016中国APP分类排行榜参选入围产品公示 由中国科学院<互联网周刊>.中国社会科学院信息化研究中心.eNet硅谷动力共同主办的2016中国APP分类排行榜发布暨颁奖晚宴即将举行.此 ...
- JavaWeb学习总结(五)—Myecplise的优化
1.设定项目的默认编码 General --> Workspace --> UTF-8 2.设定JSP的打开方式,默认会很占内存 General --> Editors --> ...