一个汉诺塔问题

K行天下 2008-09-27 11:03:51
A Double Tower of Hanoi contains twice the number of disk as the regular Tower of Hanoi
problem, where each disk size appears twice. So there are 2n disks, of n different sizes (n>0). As
usual, there are 3 pegs. The objective is to transfer the whole tower form the original peg to one of
the other two pegs, moving only one disk at a time, without putting a larger one over a smaller one.
Putting a same-sized disk onto another is okay. If we are required to reproduce the original
top-to-bottom order arrangement, how many moves (minimal) does it take? Remember, disks of
equal size need to be in original order, and cannot be inversed.
...全文
235 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
最后f(n)就是题目的解,测试结果如下:
f(1)==3
f(2)==11
f(3)==27
f(4)==59
f(5)==123
f(6)==251
f(7)==507
f(8)==1019
f(9)==2043
f(10)==4091
f(11)==8187
f(12)==16379
f(13)==32763
f(14)==65531
f(15)==131067
f(16)==262139
f(17)==524283
f(18)==1048571
f(19)==2097147
f(20)==4194299
......
  • 打赏
  • 举报
回复
用f(n)来表示移动2n个这样的盘子(没有倒置)需要的最少移动次数,
用g(n)来表示移动2n个这样的盘子(只有最大的两个同尺寸盘子倒置)的最少移动次数。
显然g(n)<f(n)

边界条件:
f(1)=3,g(1)=2
状态转移方程:
g(n)=2*g(n-1)+2
f(n)=4*g(n-1)+3
  • 打赏
  • 举报
回复
考虑不周,上面的转移方程是错的~
  • 打赏
  • 举报
回复
这题特殊的一点在于要求最后尺寸相同的盘子不能倒置。
用f(n)来表示移动2n个这样的盘子(最后尺寸相同的盘子没有倒置)需要的最少移动次数,
用g(n)来表示移动2n个这样的盘子(最后尺寸相同的盘子全部倒置)的最少移动次数。

边界条件:
f(1)=3,g(1)=2
状态转移方程:
g(n)=f(n-1)+g(n-1)+2
f(n)=4*min{f(n-1),g(n-1)}+3

sagegz 2008-09-27
  • 打赏
  • 举报
回复
LZ好久不见!

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧