汉诺塔III 汉诺塔IV 汉诺塔V (规律)
汉诺塔III
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13271 Accepted Submission(s): 6095
现在我们改变游戏的玩法,不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允许大盘放到下盘的上面。
Daisy已经做过原来的汉诺塔问题和汉诺塔II,但碰到这个问题时,她想了很久都不能解决,现在请你帮助她。现在有N个圆盘,她至少多少次移动才能把这些圆盘从最左边移到最右边?
3
12
26
531440
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<algorithm>
- using namespace std;
- const int INF=0x3f3f3f3f;
- #define mem(x,y) memset(x,y,sizeof(x))
- #define SI(x) scanf("%d",&x)
- #define SL(x) scanf("%lld",&x)
- #define PI(x) printf("%d",x)
- #define PL(x) printf("%lld",x)
- #define P_ printf(" ")
- #define T_T while(T--)
- typedef long long LL;
- LL dp[40];
- int main(){
- int T,N;
- dp[0]=0;
- for(int i=1;i<=35;i++)dp[i]=3*dp[i-1]+2;
- while(~SI(N))PL(dp[N]),puts("");
- return 0;
- }
汉诺塔IV
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5517 Accepted Submission(s): 3985
每组数据有一个正整数n(1 <= n <= 20),表示有n个盘子。
1
10
19684
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<algorithm>
- using namespace std;
- const int INF=0x3f3f3f3f;
- #define mem(x,y) memset(x,y,sizeof(x))
- #define SI(x) scanf("%d",&x)
- #define SL(x) scanf("%lld",&x)
- #define PI(x) printf("%d",x)
- #define PL(x) printf("%lld",x)
- #define P_ printf(" ")
- #define T_T while(T--)
- typedef long long LL;
- LL dp[40];
- int main(){
- int T,N;
- dp[0]=0;
- for(int i=1;i<=35;i++)dp[i]=3*dp[i-1]+2;
- SI(T);
- T_T{
- SI(N);
- PL(dp[N-1]+2);puts("");
- }
- return 0;
- }
汉诺塔V
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3801 Accepted Submission(s): 2248
题经常作为一个递归的经典例题存在。可能有人并不知道汉诺塔问题的典故。汉诺塔来源于
印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往上按大小
顺序摞着64片黄金圆盘。上帝命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱
子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一回只能移动一个圆盘。我们
知道最少需要移动2^64-1次.在移动过程中发现,有的圆盘移动次数多,有的少 。 告之盘
子总数和盘号,计算该盘子的移动次数.
号k(1<=k<=N)。
60 1
3 1
4
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<algorithm>
- using namespace std;
- const int INF=0x3f3f3f3f;
- #define mem(x,y) memset(x,y,sizeof(x))
- #define SI(x) scanf("%d",&x)
- #define SL(x) scanf("%lld",&x)
- #define PI(x) printf("%d",x)
- #define PL(x) printf("%lld",x)
- #define P_ printf(" ")
- #define T_T while(T--)
- typedef long long LL;
- LL dp[65][65];
- int main(){
- int T,N,k,cur;
- for(int i=1;i<=60;i++){
- cur=0;
- for(int j=i;j>=1;j--){
- dp[i][j]=(LL)1<<cur;
- cur++;
- }
- }
- SI(T);
- T_T{
- SI(N);SI(k);
- PL(dp[N][k]);puts("");
- }
- return 0;
- }
汉诺塔III 汉诺塔IV 汉诺塔V (规律)的更多相关文章
- 汉诺塔系列问题: 汉诺塔II、汉诺塔III、汉诺塔IV、汉诺塔V、汉诺塔VI
汉诺塔 汉诺塔II hdu1207: 先说汉若塔I(经典汉若塔问题),有三塔.A塔从小到大从上至下放有N个盘子.如今要搬到目标C上. 规则小的必需放在大的上面,每次搬一个.求最小步数. 这个问题简单, ...
- HDU 2064 汉诺塔III
汉诺塔III Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 题目1458:汉诺塔III(不一样的汉诺塔递归算法)
题目链接:http://ac.jobdu.com/problem.php?pid=1458 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- HDUOJ----(2064)汉诺塔III
汉诺塔III Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDOJ.2064 汉诺塔III
汉诺塔III Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HDU——2064汉诺塔III
汉诺塔III Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- 汉诺塔III HDU - 2064
汉诺塔III HDU - 2064 约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下.由小到大顺序串着由64个圆盘构成的塔.目的是将最左边杆上的盘全部移到右 ...
- 汉诺塔III 递推题
题目描述: 约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下.由小到大顺序串着由64个圆盘构成的塔.目的是将最左边杆上的盘全部移到右边的杆上,条件是一次只能移动 ...
- HDU 2064 汉诺塔III(递归)
题目链接 Problem Description 约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下.由小到大顺序串着由64个圆盘构成的塔.目的是将最左边杆上的盘 ...
随机推荐
- iOS中不透明度的查看
模拟器工具条 Debug-->Color Blended Layers 即中文显示下 调试 -->颜色混合层 绿色代表不透明部分,红色代表透明部分,红色越多对性能影响越大
- undo_retention:确定最优的撤销保留时间
使用下面的公式来计算undo_retention参数的值: undo_retention=undo size/(db_block_size * undo_block_per_sec) 可以通过提交下面 ...
- jsonarray----->list
JSONArray--------------->List----------------->Adapter------------------>ListView
- SQL PLUS远程连接
http://blog.csdn.net/wildin/article/details/5850252 这篇文章无敌了. Oracle sqlplus添加历史记录功能: http://www.cnbl ...
- select函数详解及应用
Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如connect. accept.recv或recvfrom这样的阻塞程序 ...
- 浅析C++内存分配与释放操作过程——三种方式可以分配内存new operator, operator new,placement new
引言:C++中总共有三种方式可以分配内存,new operator, operator new,placement new. 一,new operator 这就是我们最常使用的 new 操作符.查看汇 ...
- java转换字符串的编码(转)
package com.Alex.base; import java.io.UnsupportedEncodingException; /** * 转换字符串的编码 */ public class C ...
- HDU4738【杭州网赛、判桥】
刚拿到这道题时挺有思路,无奈平日里只敲过找割顶的代码,判桥的代码当时自己也没仔细敲. 当时一把泪啊,忽然感觉自己的图论才只是刚搞了个起步啊.. 题目有神坑. 就是先判是否连通,不连通直接输出0; ...
- 网易云课堂_程序设计入门-C语言_第二周:判断_2信号报告
2 信号报告(5分) 题目内容: 无线电台的RS制信号报告是由三两个部分组成的: R(Readability) 信号可辨度即清晰度. S(Strength) 信号强度即大小. 其中R位于报告第一 ...
- php将文件夹打包成zip文件
function addFileToZip($path,$zip){ $handler=opendir($path); //打开当前文件夹由$path指定. while(($filenam ...