题目链接:http://sfxb.openjudge.cn/dongtaiguihua/E/

题目描述:4个柱子的汉诺塔,求盘子个数n从1到12时,从A移到D所需的最大次数。限制条件和三个柱子的汉诺塔问题相同。

解题思路:采用动态规划算法的思路为先从将k个盘子使用4个柱子的方法从A移到B,然后将A上剩下的n-k个盘子使用3个柱子的方法移到D上,然后再使用4个柱子的方法将B上的k个盘子移到D上。可以明白这道题会产生很多重复子问题。所以先计算出使用3个柱子的方法从A移到B的次数用数组f3保存。然后计算n=1到12时,k从1到i-1变化时,f4[n]的大小。计算过程满足下边的方程f4[n]=min(1≤k<i)(f3[k]+2*f[i-k]);当n=1时,f4[1]=1;

代码如下:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
using namespace std;
int f3[];
int f4[];
int three_hoi(int x){
if(x==)return ;
return *three_hoi(x-)+;
}
int main(){
for(int i=;i<=;i++){
f3[i]=three_hoi(i);
}
f4[]=;
// memset(f4,-1,sizeof(f4));
for(int i=;i<=;i++){
f4[i]=;
}
for(int i=;i<=;i++){
for(int j=i-;j>=;j--){
int t=f3[j]+*f4[i-j];
if(t<f4[i]){
// cout<<f4[i]<<"f4";
f4[i]=t;
// cout<<i<<"i"<<j<<"j";
// cout<<t<<"t"<<endl;
}
}
}
// for(int i=1;i<=12;i++){
// printf("%d\n",f3[i]);
// }
// system("pause");
for(int i=;i<=;i++){
printf("%d\n",f4[i]);
}
return ;
}

对于给定的n,算法时间复杂度为O(n).

Strange Towers of Hanoi的更多相关文章

  1. POJ 1958 Strange Towers of Hanoi 解题报告

    Strange Towers of Hanoi 大体意思是要求\(n\)盘4的的hanoi tower问题. 总所周知,\(n\)盘3塔有递推公式\(d[i]=dp[i-1]*2+1\) 令\(f[i ...

  2. POJ 1958 Strange Towers of Hanoi

    Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3784 Accepted: 23 ...

  3. POJ-1958 Strange Towers of Hanoi(线性动规)

    Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2677 Accepted: 17 ...

  4. POJ1958 Strange Towers of Hanoi [递推]

    题目传送门 Strange Towers of Hanoi Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3117   Ac ...

  5. poj1958——Strange Towers of Hanoi

    The teacher points to the blackboard (Fig. 4) and says: "So here is the problem: There are thre ...

  6. poj1958 strange towers of hanoi

    说是递推,其实也算是个DP吧. 就是4塔的汉诺塔问题. 考虑三塔:先从a挪n-1个到b,把最大的挪到c,然后再把n-1个从b挪到c,所以是 f[i] = 2 * f[i-1] + 1; 那么4塔类似: ...

  7. Strange Towers of Hanoi POJ - 1958(递推)

    题意:就是让你求出4个塔的汉诺塔的最小移动步数,(1 <= n <= 12) 那么我们知道3个塔的汉诺塔问题的解为:d[n] = 2*d[n-1] + 1 ,可以解释为把n-1个圆盘移动到 ...

  8. POJ1958:Strange Towers of Hanoi

    我对状态空间的理解:https://www.cnblogs.com/AKMer/p/9622590.html 题目传送门:http://poj.org/problem?id=1958 题目要我们求四柱 ...

  9. 【POJ 1958】 Strange Towers of Hanoi

    [题目链接] http://poj.org/problem?id=1958 [算法] 先考虑三个塔的情况,g[i]表示在三塔情况下的移动步数,则g[i] = g[i-1] * 2 + 1 再考虑四个塔 ...

随机推荐

  1. ie兼容---haslayout

    要想更好的理解 css, 尤其是 IE 下对 css 的渲染,haslayout 是一个非常有必要彻底弄清除的概念.大多IE下的显示错误,就是源于 haslayout. 什么是 haslayout ? ...

  2. scrollTop,offset().top

    1.scrollTop是指滚动条滚动的距离 如果没有出现滚动条,则距离为0 css: <style type="text/css"> *{ margin: 0; pad ...

  3. python 学习day6(面向对象)

    博客部分内容转自:http://www.cnblogs.com/wupeiqi/p/4493506.html 面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法 ...

  4. HTTP协议漫谈(转)

    转自:http://www.cnblogs.com/CareySon/archive/2012/04/27/HTTP-Protocol.html HTTP的定义和历史 在一个网络中.传输数据需要面临三 ...

  5. RoHS认证简介

    RoHS认证是<电气.电子设备中限制使用某些有害物质指令>(The restriction of the use of certain hazardous substances in el ...

  6. DirectUI实现原理

    一,概念 传统的Windows窗口程序对每一个控件都会创建一个句柄,而DUI技术奖所有控件都绘制在一个窗体上,这些控件的逻辑和绘图方式必须自己进行编写和封装,所以这些控件都是无句柄的. DUI技术的实 ...

  7. draw lines on ColumnChart

    原文 http://blog.csdn.net/lixuekun820/article/details/5485042 Summary: Adobe 的 Flex Chart提供了很强大的功能,通过简 ...

  8. Javascript基本算法演练 Seek and Destroy

    转载自:http://aeroj-blog.logdown.com/posts/435808 Seek and Destroy You will be provided with an initial ...

  9. aix puppet agent

    demo控制脚本tel,150 5519 8367 Running Puppet on AIX Puppet on AIX is… not officially supported, yet stil ...

  10. Spring、编码剖析Spring管理Bean的原理

    引入dom4j jar包 1.新建Person接口和PersonBean public interface PersonIService { public void helloSpring(); } ...