博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1958
阅读量:6999 次
发布时间:2019-06-27

本文共 677 字,大约阅读时间需要 2 分钟。

题意:4柱子hanoi,给出n<=12,求最少步数。

分析:我们知道3柱子的hanoi,步数=2^n-1。题中给出四个的解题思路,用dp求解,题中说先把n分成两部分,A部分用4柱子方法挪到2柱子,B部分用3柱子法挪到4柱子,然后用4柱子法把A挪到4柱子。假设B部分有k个,则g[i] = g[i - j] * 2 + f[j];

g[i]表示4柱子法,f[i]是3柱子法,挪动i个盘子的步数。

View Code
#include 
#include
#include
#include
usingnamespace std; int f[15], g[15]; int main() {
//freopen("t.txt", "r", stdin); f[0] =0; int ans; for (int i =1; i <=12; i++) f[i] = (1<< i) -1; g[0] =0; for (int i =1; i <=12; i++) {
g[i] =0x3f3f3f3f; for (int j =1; j <= i; j++) g[i] = min(g[i], g[i - j] *2+ f[j]); printf("%d\n", g[i]); } return0; }

转载地址:http://sievl.baihongyu.com/

你可能感兴趣的文章
解决Mac OS 山猫10.8下Xcode无法更新以及速度慢的问题
查看>>
poj 1988 Cube Stacking(并查集)
查看>>
lamp 403
查看>>
关于“鸡脚神”的看法
查看>>
c# 路径空格---ProcessStartInfo参数问题
查看>>
我的MYSQL学习心得(十七) 复制
查看>>
用eclipse建立servlet工程
查看>>
MySql通用分页存储过程
查看>>
LoadRunner脚本回放日志中的Warning信息
查看>>
Surround the Trees(凸包求周长)
查看>>
转载:如何运用VI编辑器进行查找替换
查看>>
android xutils
查看>>
strut2.xml中result param详细设置
查看>>
Mysql注入绕过姿势
查看>>
移动互联网实战--社交游戏的排行榜设计和实现(2)
查看>>
不要再用if(xxx != null)或者try catch NullPointerException了,Optional可以帮你解决
查看>>
excel读写技术二
查看>>
Panorama控件和Pivot控件【WP7学习札记之十四】
查看>>
dialog shell下的gui设计 代替繁杂libncurses编程
查看>>
Ubuntu Linux 下文件名乱码(无效的编码)的快速解决办法
查看>>