Uva 10795 A Different Task 解题报告(递归+思维)
来源:互联网 发布:mt3601 数据手册 编辑:程序博客网 时间:2024/04/30 21:07
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
31 1 12 2 231 2 33 2 141 1 1 11 1 1 10
Sample Output
Case 1: 7Case 2: 3Case 3: 0
解题报告: 《训练指南》例题。很考思维。
看书上的方法做的。最考思维的地方应该是对于初始态转移到参考态,与参考态转移到目标态。这里直接逆向思维了,由目标态到参考态。
而中间让时间大大缩短的是,i-1个盘的整体移动,直接使用经典结论了。觉得要好好思考下才能掌握这题的精髓……
代码如下:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <vector>#include <queue>#include <map>#include <string>using namespace std;#define ff(i, n) for(int i=0;i<(n);i++)#define fff(i, n, m) for(int i=(n);i<=(m);i++)#define dff(i, n, m) for(int i=(n);i<=(m);i++)typedef long long LL;typedef unsigned long long ULL;void work();int main(){#ifdef ACM freopen("in.txt", "r", stdin);#endif // ACM work();}///////////////////////////////////////////LL ans;int goal[66];int now[66];LL move(int * state, int pan, int pos){ if(pan==0) return 0; if(state[pan] == pos) return move(state, pan-1, pos); return move(state, pan-1, 6-pos-state[pan])+(1LL << (pan-1));}void work(){ int n; int cas = 1; while(~scanf("%d", &n) && n) { ff(i, n) scanf("%d", now+i+1); ff(i, n) scanf("%d", goal+i+1); while(n>=1 && now[n]==goal[n]) n--; LL ans = 0; if(n>0) { int pos; if(now[n] == goal[n]) pos = now[n]; else pos = 6-now[n]-goal[n]; ans = move(now, n-1, pos)+move(goal, n-1, pos)+1; } printf("Case %d: %lld\n", cas++, ans); }}
- Uva 10795 A Different Task 解题报告(递归+思维)
- uva 10795 - A Different Task(递归)
- uva 10795 - A Different Task(递归)
- UVA 10795 - A Different Task(递归)
- uva 10795 - A Different Task (递归+状态转移)
- uva 10795 A Different Task(递归模拟)
- UVA 10795 A Different Task(递归 状态转移)
- UVa --- 10795 A Different Task 汉诺塔不一样的操作【递归 + 思维】
- 【Jason's_ACM_解题报告】A Different Task
- uva 10795A Different Task(递归求解另一种汉诺塔)
- Uva 10795 A Different Task
- Uva-10795-A Different Task
- uva 10795 A Different Task
- UVA 10795 A Different Task
- UVA 10795 A Different Task
- Uva 10795 - A Different Task
- UVA - 10795 A Different Task
- Uva 10795 A Different Task
- "error":400,"reason":"Bad Request","message":"No values provided for the request parameter '_action
- 谈谈我为何不再用.NET的几点原因
- Hibernate添加 annotation注解实体【AddHibernate mapping annotations to POJO 灰色点不了】
- 7-12 [NWPU][2014][TRN][3]搜索 D - 广搜 基础 POJ 1915
- HOJ 1867 经理的烦恼
- Uva 10795 A Different Task 解题报告(递归+思维)
- 【入坑】又要开始写解题报告了
- jquery 和REST模式
- Uva 340 Master-Mind Hints
- poj3468 第二颗线段树。。。
- linux网络编程常用函数详解与实例(socket-->bind-->listen-->accept)
- Windows黑客编程基础
- (插播)网络爬虫,抓取你想要得东西。
- AutoCAD2015 优化设置方案