说是递推,其实也算是个DP吧。

就是4塔的汉诺塔问题。

考虑三塔:先从a挪n-1个到b,把最大的挪到c,然后再把n-1个从b挪到c,所以是

f[i] = 2 * f[i-1] + 1;

那么4塔类似:

先在4塔模式下挪j个到b,然后在三塔模式下挪n-j个到d,然后再在4塔模式下挪j个到d。

故状态转移方程:

d[i] = min(2 * d[j] + f[n-j]) ,其中1 <= j < n

初始条件:f[n]与d[1] = 1;

然后就没有然后了。

 #include <cstdio>
#include <algorithm>
using namespace std;
const int N = ,INF=0x7f7f7f7f;
int f[N],d[N];
int main()
{
f[]=d[]=;
f[]=d[]=;
for(int i=;i<=;i++) {
f[i]=*f[i-]+;
}
for(int i=;i<=;i++)
{
int temp=INF;
for(int j=;j<i;j++)
temp=min(temp,*d[j]+f[i-j]);
d[i]=temp;
}
for(int i=;i<=;i++)
printf("%d\n",d[i]); return ;
}

AC代码

poj1958 strange towers of hanoi的更多相关文章

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

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

  2. POJ1958 Strange Towers of Hanoi [递推]

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

  3. poj1958——Strange Towers of Hanoi

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

  4. 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 ...

  5. POJ 1958 Strange Towers of Hanoi

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

  6. POJ1958:Strange Towers of Hanoi

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

  7. [POJ1958][Strange Tower of Hanoi]

    题目描述 求解 \(n\) 个盘子 \(4\) 座塔的 Hanoi 问题最少需要多少步 问题分析 考虑 \(3\) 座塔的 Hanoi 问题,记 \(f[i]\) 表示最少需要多少步, 则 \(f[i ...

  8. Strange Towers of Hanoi

    题目链接:http://sfxb.openjudge.cn/dongtaiguihua/E/ 题目描述:4个柱子的汉诺塔,求盘子个数n从1到12时,从A移到D所需的最大次数.限制条件和三个柱子的汉诺塔 ...

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

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

随机推荐

  1. Day 4-1 模块的导入方法和路径

    什么是模块? 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码 ...

  2. Selenium简单回顾

    一.Selenium介绍 1.Selenium(浏览器自动化测试框架): Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的 ...

  3. MyBatis基础:MyBatis动态SQL(3)

    1. 概述 MyBatis中动态SQL包括元素: 元素 作用 备注 if 判断语句 单条件分支判断 choose(when.otherwise) 相当于Java中的case when语句 多条件分支判 ...

  4. 老男孩python学习自修第二十四天【多进程】

    1. 体验多进程的运行速度 #!/usr/bin/env python # _*_ coding:UTF-8 _*_ from multiprocessing import Pool import t ...

  5. html5 autoplay不起作用 Uncaught (in promise) DOMException: play() failed because the user didn't interac

    chrome://flags/#autoplay-policy

  6. JUC虚假唤醒(六)

    为什么条件锁会产生虚假唤醒现象(spurious wakeup)? ​ 在不同的语言,甚至不同的操作系统上,条件锁都会产生虚假唤醒现象.所有语言的条件锁库都推荐用户把wait()放进循环里: whil ...

  7. time模块 转换关系图

    import time t = time.time() #获取目前时间 t_struck = time.localtime(t) #time.gmtime() utc时区 t_str = time.s ...

  8. yolo算法解析

  9. Multi-Targeting and Porting a .NET Library to .NET Core 2.0

    Creating a new .NET Standard Project The first step for moving this library is to create a new .NET ...

  10. How to execute a Stored Procedure with Entity Framework Code First

    Recently I worked on a project, which I started as code first and then I forced to switch to Databas ...