UVA 10795 - A Different Task(递归)
来源:互联网 发布:网络电视盒安装 编辑:程序博客网 时间:2024/05/01 04:44
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
题意:给定一个汉若塔初始和目标,求最少步数。
思路:每次先移动大的,然后其他的肯定要先全部堆到没用的柱子上,然后最后在一个个去放回位置
代码:
#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);elsereturn 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(递归)
- uva 10795 - 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
- 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
- 好友的blog
- OS X QT App redistributable bundle 部署教程
- Direct9学习之--------------------------动画<骨骼动画>
- Direct3D VertexBuffer Lock() and Unlock() function
- VC 6.0 调试技巧
- UVA 10795 - A Different Task(递归)
- fork 与 vfork区别
- dos获取键盘输入
- Linux 性能监控常用命令
- WCF服务编程-HelloWorld(转载)
- 三维渲染必备常识
- HibernateTemplate的使用
- Msys/MinGW与Cygwin/gcc
- ubuntu eclipse 相关设置