POJ 3572 Hanoi Towers (YY + 推公式)
来源:互联网 发布:cjson 解析json数组 编辑:程序博客网 时间:2024/05/14 21:24
Description
The “Hanoi Towers” puzzle consists of three pegs (that we will nameA, B, and C) with n disks of different diameters stacked onto the pegs. Initially all disks are stacked onto pegA with the smallest disk at the top and the largest one at the bottom, so that they form a conical shape on pegA.
A valid move in the puzzle is moving one disk from the top of one (source) peg to the top of the other (destination) peg, with a constraint that a disk can be placed only onto an empty destination peg or onto a disk of a larger diameter. We denote a move with two capital letters — the first letter denotes the source disk, and the second letter denotes the destination disk. For example,AB is a move from disk A to disk B.
The puzzle is considered solved when all the disks are stacked onto either pegB (with pegs A and C empty) or onto peg C (with pegsA and B empty). We will solve this puzzle with the following algorithm.
All six potential moves in the game (AB,AC, BA, BC, CA, and CB) are arranged into a list. The order of moves in this list defines our strategy. We always make the first valid move from this list with an additional constraint that we never move the same disk twice in a row.
It can be proven that this algorithm always solves the puzzle. Your problem is to find the number of moves it takes for this algorithm to solve the puzzle using a given strategy.
Input
The input file contains two lines. The first line consists of a single integer numbern (1 ≤ n ≤ 30) — the number of disks in the puzzle. The second line contains descriptions of six moves separated by spaces — the strategy that is used to solve the puzzle.
Output
Write to the output file the number of moves it takes to solve the puzzle. This number will not exceed 1018.
Sample Input
#1 3 AB BC CA BA CB AC#2 2 AB BA CA BC CB AC
Sample Output
#1 7#2 5
Source
题目链接:http://poj.org/problem?id=3572
题目大意:n个盘子的汉诺塔游戏,移动策略已经给出,同一个盘子不能连续被移动两次,每次都按策略中最先可行的指令进行操作,问按照给定策略完成游戏的操作次数,完成是移动到B或C都可以
题目分析:记录一下每个策略的移动指令,去掉那些将一个盘子连续移动超过一次的指令,我是直接通过3个盘子和2个盘子的情况yy的,和普通汉诺塔问题类似,考虑最小的盘子的移动方案,因为全都移到B和移到C本质相同,所以只考虑移到C的情况,可以得到三种情况
1.A -> C;C -> B;B -> A;A -> C这就是普通3层汉诺塔的最优解 2^n - 1 (2的时候3步,3的时候需要7步)
2.A -> C;C -> B;B -> C;C -> B;B -> C 推出来是3^(n-1) (2的时候3步,可以看出2的时候小盘子只移动两次即可和方案1相同,3的时候需要9步)
3.A -> C;C -> A;A -> C;... 推出来是2 * 3^(n-1) - 1 (2的时候5步,3的时候需要17步)
分情况讨论即可
#include <cstdio>#include <cstring>#include <cmath>#define ll long longint n, mp[55];char s[10];int main() { while(scanf("%d", &n) != EOF) { memset(mp, 0, sizeof(mp)); for(int i = 0; i < 6; i++) { scanf("%s", s); int a = s[0] - 'A' + 1, b = s[1] - 'A' + 1; if(!mp[a]) mp[a] = b; } if(mp[2] != 1 && mp[3] != 1) printf("%lld\n", (ll)(pow(3ll, n - 1))); else if(mp[mp[1]] == 1) printf("%lld\n", 2ll * (ll)pow(3ll, n - 1) - 1ll); else printf("%lld\n", (ll)(pow(2ll, n)) - 1ll); }}
- POJ 3572 Hanoi Towers (YY + 推公式)
- POJ 1958 Strange Towers of Hanoi (线性dp 推公式)
- Hanoi Towers - POJ 3572 dp
- POJ 3572 Hanoi Towers 可能会
- poj 1958 Strange Towers of Hanoi(递推)
- POJ 3572 Hanoi Towers(dp)
- poj 1920 Towers of Hanoi
- poj 1920 Towers of Hanoi
- POJ 1920 Towers of Hanoi
- POJ 1920 :Towers of Hanoi
- POJ 1958 Strange Towers of Hanoi [dp]
- POJ 1920 Towers of Hanoi 数论
- POJ 1958 Strange Towers of Hanoi
- poj 1958 Strange Towers of Hanoi
- POJ 1958 Strange Towers of Hanoi
- poj 1958 Strange Towers of Hanoi
- POJ 1920 Towers of Hanoi 笔记
- Hanoi Towers
- 串口中断相关问题
- Eclipse创建WEB工程Maven+Spring+SpringMVC+MyBatis示例
- 开发项目准备举例
- Magento设置部分产品的Group Price的值
- 即时聊天技术点
- POJ 3572 Hanoi Towers (YY + 推公式)
- 负载均衡反向代理模式优点及缺点
- 面试题——Hibernate中Inverse和Cascade的区别
- HAVING 语法
- 回调
- 连接池的概念
- 学生选课系统-管理员功能模块设计(Java+NetBeans+SQLServer2008)
- WEB 前端面试题(1)
- case语法