HDOJ 汉诺塔系列(递推分析)
汉诺塔V
http://acm.hdu.edu.cn/showproblem.php?pid=1995
设当有N个盘子时,第i个盘子从一个柱子移到另一个柱子需要移动的步数为f[n,p],则有:当N=p时(即p是最底下的那个盘子),f[n,p]=1;而当N!=p时,p要跟着上面N-1个盘子先移动到B柱子,等N移到C后再移到C柱子。所以此时f[n,p]=2*f[n-1,p]。
#include <fstream> #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <iomanip> #include <iomanip> #include <climits> #include <vector> #include <stack> #include <queue> #include <list> #include <set> #include <map> #include <algorithm> #include <string> #include <cstring>using namespace std;long long p_hanno(int n,int p) {if (n==p) return 1;elsereturn 2*p_hanno(n-1,p); }int main() {int t;cin>>t;while(t--){int n;int p;cin>>n>>p;cout<<p_hanno(n,p)<<endl;}return 0; }
汉诺塔III
http://acm.hdu.edu.cn/showproblem.php?pid=2064
只允许先从中间过渡。那么设b[i]为i个盘子从两边(中间)移到中间(两边)(模拟计算一下发现两个一样的)的步数。
所以容易有a[i]=2*b[i-1]+1+2*b[i-1]+1+2*b[i-1]。(把i个盘子从A移到C需要先把前i-1个盘子A->B->C,再让i移到B,再让前i-1个盘子移动C->B->A,然后i从B->C,然后前i-1从A->B->C,完成)即,a[i]=6*b[i-1]+2; 而容易得到:b[i]=3*b[i-1]+1。
#include <fstream> #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <iomanip> #include <iomanip> #include <climits> #include <vector> #include <stack> #include <queue> #include <list> #include <set> #include <map> #include <algorithm> #include <string> #include <cstring>using namespace std;long long B(int n) {if (n==1) return 1;elsereturn 3*B(n-1)+1; }long long A(int n) {if (n==1) return 2;elsereturn 6*B(n-1)+2; }int main() {int n;while(cin>>n){cout<<A(n)<<endl;}return 0; }
汉诺塔IV
http://acm.hdu.edu.cn/showproblem.php?pid=2077
和Ⅲ类似,如果允许最大的放上面,则只需要前i-1从A->B,i从A->B->C,前i-1从B->C。所以跟上面比只有a[i]=2*b[i-1]+2不一样。
#include <fstream> #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <iomanip> #include <iomanip> #include <climits> #include <vector> #include <stack> #include <queue> #include <list> #include <set> #include <map> #include <algorithm> #include <string> #include <cstring>using namespace std;long long B(int n) {if (n==1) return 1;elsereturn 3*B(n-1)+1; }long long A(int n) {if (n==1) return 2;elsereturn 2*B(n-1)+2; }int main() {int n;int t;cin>>t;while(t--){cin>>n;cout<<A(n)<<endl;}return 0; }
汉诺塔VI
http://acm.hdu.edu.cn/showproblem.php?pid=1996
水题本性。允许不按最优步骤走,计算有多少种序列。n个盘子,每个盘子都可以选择三个柱子,即3^n。
#include <fstream> #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <iomanip> #include <iomanip> #include <climits> #include <vector> #include <stack> #include <queue> #include <list> #include <set> #include <map> #include <algorithm> #include <string> #include <cstring>using namespace std;long long hanno6(int n) {if (n==0) return 1;elsereturn 3*hanno6(n-1); }int main() {int n;int t;cin>>t;while(t--){cin>>n;cout<<hanno6(n)<<endl;}return 0; }
转载于:https://www.cnblogs.com/AbandonZHANG/archive/2012/07/28/2613688.html
HDOJ 汉诺塔系列(递推分析)相关推荐
- hdu 2064汉诺塔III 递推
汉诺塔递推题,比汉诺塔多了一个限制条件,盘子只允许在相邻的柱子之间移动. 分析: 第1步:初始状态: 第2步:把上面的n-1个盘移到第3号杆上: 第3步:把第n个盘从1移到2: 第4步:把前n-1个从 ...
- 《算法竞赛进阶指南》打卡-基本算法-AcWing 96. 奇怪的汉诺塔:递推
文章目录 题目解答 题目链接 题目解答 来源:acwing 分析: 本题的汉诺塔问题是n个盘子4个塔,最基本的汉诺塔是n个盘子3个塔.本题是要在后者的基础上来做. 设d[i]表示i盘3塔问题的最小移动 ...
- 96. 奇怪的汉诺塔【递推】
这是一道很经典的问题. 先看3个塔的情况 需要注意的是,这里的汉诺塔移动并无只可以移动到相邻塔的需求.即可以直接从A移到C. 设d[i]表示将i个盘子从这里移到其他除它之外塔的步数 ,总塔的数量为3 ...
- 汉诺塔VII(递推,模拟)
汉诺塔VII Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- 四柱子汉诺塔—递归—递推
三塔:递推式:d[n] = 2 * d[n-1] + 1 即把前n-1个盘子从A柱移到B柱,然后把A柱上剩的那一个盘子移动到C柱,最后把B柱上的那n-1个盘子移动到C柱上 四塔:递推式:f[n] = ...
- sdut 汉诺塔系列2
汉诺塔系列2 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description 用1,2,.. ...
- 汉诺塔系列问题: 汉诺塔II、汉诺塔III、汉诺塔IV、汉诺塔V、汉诺塔VI、汉诺塔VII
汉诺塔II:(hdu1207) /先说汉若塔I(经典汉若塔问题),有三塔,A塔从小到大从上至下放有N个盘子,现在要搬到目标C上, 规则小的必需放在大的上面,每次搬一个,求最小步数.这个问题简单,DP: ...
- 汉诺塔系列问题: 汉诺塔II、汉诺塔III、汉诺塔IV、汉诺塔V、汉诺塔VI
汉诺塔 汉诺塔II hdu1207: 先说汉若塔I(经典汉若塔问题),有三塔,A塔从小到大从上至下放有N个盘子,现在要搬到目标C上, 规则小的必需放在大的上面,每次搬一个,求最小步数.这个问题简单,D ...
- 汉诺塔递归问题的分析与Python实现
背景 相传在古印度圣庙中,有一种被称为汉诺塔(Hanoi)的游戏.该游戏是在一块铜板装置上,有三根杆(编号A.B.C),在A杆自下而上.由大到小按顺序放置64个金盘(如图).游戏的目标:把A杆上的金盘 ...
最新文章
- Load 方法 暨 程序的加载顺序
- 案例九:shell脚本自动创建多个新用户,并设置密码
- Android 开发 Activity里获取View的宽度和高度 转载
- python3 列表的增删改查
- vs如何写多线程_VS + PS + GS
- 微电子科学与工程要学计算机吗,微电子科学与工程专业就业前景如何 有前途吗...
- forth day ---内置函数、匿名函数
- IOS上路_04-简单示例-图片浏览器
- 累计增量备份策略_SAN存储做定时/实时备份的介绍
- js的5种继承方式——前端面试
- 别把SEO当苦力活,做优化要讲究策略
- search:笔记本插上耳机还是外放?(Dell win10)
- (转) 在C#中使用WIA获取扫描仪数据(三、利用Filter处理图片)
- 初遇QT——遇见QT遇见你,不负代码不负卿
- swagger配置使用及安全方案
- 对于分布式集群,应该思考什么问题?
- phpredis 和 predis
- Facebook购买帐号
- WebSocket和SocketIO总结
- Android Studio的gradle报错“The error message “No variants found for ‘demo‘. Check build files to ensur“