The Towers of Hanoi Revisited---(多柱汉诺塔)
Description
You all must know the puzzle named "The Towers of Hanoi". The puzzle has three pegs and N discs of different radii, initially all disks are located on the first peg, ordered by their radii - the largest at the bottom, the smallest at the top. In a turn you may take the topmost disc from any peg and move it to another peg, the only rule says that you may not place the disc atop any smaller disk. The problem is to move all disks to the last peg making the smallest possible number of moves.
There is the legend that somewhere in Tibet there is a monastery where monks tirelessly move disks from peg to peg solving the puzzle for 64 discs. The legend says that when they finish, the end of the world would come. Since it is well known that to solve the puzzle you need to make 2N - 1 moves, a small calculation shows that the world seems to be a quite safe place for a while.
However, recent archeologists discoveries have shown that the things can be a bit worse. The manuscript found in Tibet mountains says that the puzzle the monks are solving has not 3 but M pegs. This is the problem, because when increasing the number of pegs, the number of moves needed to move all discs from the first peg to the last one following the rules described, decreases dramatically. Calculate how many moves one needs to move N discs from the first peg to the last one when the puzzle has M pegs and provide the scenario for moving the discs.
Input
Output
On the first line output L - the number of moves needed to solve the puzzle. Next L lines must contain the moves themselves. For each move print the line of the form
move <disc-radius> from <source-peg> to <target-peg>
if the disc is moved to the empty peg or
move <disc-radius> from <source-peg> to <target-peg> atop <target-top-disc-radius>
if the disc is moved atop some other disc.
Disc radii are integer numbers from 1 to N, pegs are numbered from 1 to M.
Sample Input
5 4
Sample Output
13
move 1 from 1 to 3
move 2 from 1 to 2
move 1 from 3 to 2 atop 2
move 3 from 1 to 4
move 4 from 1 to 3
move 3 from 4 to 3 atop 4
move 5 from 1 to 4
move 3 from 3 to 1
move 4 from 3 to 4 atop 5
move 3 from 1 to 4 atop 4
move 1 from 2 to 1
move 2 from 2 to 4 atop 3
move 1 from 1 to 4 atop 2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 1e2+;
const double eps = 1e-;
const int INF = 1e8+;
int f[MAXN][MAXN], p[MAXN][MAXN];///f:步数 p:节点
void get(int n, int k)
{
if(f[n][k] != -)
return;
f[n][k] = INF;
if(k < )
return;
for(int m=; m<n; m++)
{
get(m, k);
get(n-m, k-);
int tp = *f[m][k]+f[n-m][k-];
if(f[n][k] > tp)
{
f[n][k] = tp;
p[n][k] = m;
}
}
}
int n, m;
int hanoi[MAXN][MAXN], num[MAXN];
void print(int s, int t, int a, int b)
{
if(a == )
{
printf("move %d from %d to %d ",hanoi[s][num[s]]+,s,t);
if(num[t])
printf("atop %d",hanoi[t][num[t]]+);
puts("");
num[t]++;
hanoi[t][num[t]]=hanoi[s][num[s]--];
return;
}
for(int i=; i<=m; i++)
{
if(i!=s && i!=t)
{
if(hanoi[i][num[i]] > hanoi[s][num[s]-p[a][b]+])
{
print(s, i, p[a][b], b);
print(s, t, a-p[a][b], b-);
print(i, t, p[a][b], b);
return;
}
}
}
return ;
}
int main()
{
while(cin>>n>>m)
{
memset(f, -, sizeof(f));
for(int i=; i<=m; i++)
f[][i] = ;
get(n, m);
cout<<f[n][m]<<endl;
memset(hanoi, , sizeof(hanoi));
memset(num, , sizeof(num));
for(int i=n; i>=; i--)
{
hanoi[][num[]] = i;
num[]++;
}
for(int i=; i<=m; i++)
hanoi[i][] = INF;
print(, m, n, m);
}
return ;
}
The Towers of Hanoi Revisited---(多柱汉诺塔)的更多相关文章
- 4柱汉诺塔(zz)
多柱汉诺塔可以用Frame–Stewart算法来解决. The Frame–Stewart algorithm, giving a presumably optimal solution for fo ...
- 多柱汉诺塔问题“通解”——c++
多柱汉诺塔问题 绪言 有位同学看到了我的初赛模拟卷上有一道关于汉诺塔的数学题.大概就是要求4柱20盘的最小移动次数. 他的数学很不错,找到了应该怎样推. 如果要把n个盘子移到另一个柱子上,步骤如下: ...
- hdu 1207 四柱汉诺塔
递推,汉诺塔I的变形. 这题真心没想到正确解法,越想越迷糊.这题看了别人题解过得,以后还是自己多想想,脚步太快并非好事. 贴上分析: 分析:设F[n]为所求的最小步数,显然,当n=1时,F[n]= ...
- SGU 202. The Towers of Hanoi Revisited
多柱汉诺塔问题. 引用自wiki百科 多塔汉诺塔问题 在有3个柱子时,所需步数的公式较简单,但对于4个以上柱子的汉诺塔尚未得到通用公式,但有一递归公式(未得到证明,但目前为止没有找到反例): 令为在有 ...
- 四柱加强版汉诺塔HanoiTower----是甜蜜还是烦恼
我想很多人第一次学习递归的时候,老师或者书本上可能会举汉诺塔的例子. 但是今天,我们讨论的重点不是简单的汉诺塔算法,而是三柱汉诺塔的延伸.先来看看经典的三柱汉诺塔. 一.三柱汉诺塔(Hanoi_Thr ...
- 汉诺塔的问题:4个柱子,如果塔的个数变位a,b,c,d四个,现要将n个圆盘从a全部移到d,移动规则不变
四柱汉诺塔问题的求解程序.解题思路:如a,b,c,d四柱. 要把a柱第n个盘移到目标柱子(d柱),先把上层 分两为两部份,上半部份移到b柱,下半部分移到c柱,再把第n盘移到 目标柱子,然后,c柱盘子再 ...
- HDU汉诺塔系列
这几天刷了杭电的汉诺塔一套,来写写题解. HDU1207 汉诺塔II HDU1995 汉诺塔V HDU1996 汉诺塔VI HDU1997 汉诺塔VII HDU2064 汉诺塔III HDU2077 ...
- [递推]B. 【例题2】奇怪汉诺塔
B . [ 例 题 2 ] 奇 怪 汉 诺 塔 B. [例题2]奇怪汉诺塔 B.[例题2]奇怪汉诺塔 题目描述 汉诺塔问题,条件如下: 这里有 A A A. B B B. C C C 和 D D D ...
- zoj 2338 The Towers of Hanoi Revisited
The Towers of Hanoi Revisited Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge You all mus ...
随机推荐
- 使用WebApi时Post和Put的区别
简单的说Post是添加,Put是修改 吃不准的话,尝试用相同参数访问二次接口,结果不同的是Post(会产生多条记录),结果相同的是Put(仅为一条记录),例如:写博客就是Post:更新签名就是Put
- LINUNX下PHP下载中文文件名代码
function get_basename($filename){ return preg_replace('/^.+[\\\\\\/]/', '', ...
- GitHub 操作流程示例
最新文章:Virson's Blog 参考文章: 博客园-Web前端开发,博客园-喻头快跑,GotGitHub 首先.通过github网站新建一个仓库,得到仓库地址 https://github.co ...
- OceanBase server处理网络包的回调逻辑
OceanBase处理网络包的逻辑还是蛮绕的,这里以UPS为例,作为给自己的备忘. UPS代码的main.cpp中调用ObUpdateServerMain的start启动server.start函数会 ...
- 自动化测试管理平台ATMS(V2.0.1_8.12)下载
自动化测试管理平台ATMS(V2.0.1_8.12)下载: http://automationqa.com/forum.php?mod=viewthread&tid=2701&from ...
- [LeetCode] Additive Number
Af first I read the title as "Addictive Number". Anyway, this problem can be solved elegan ...
- 使用hessian+protocol buffer+easyUI综合案例--登陆
首先先简单介绍下hessian ,protocol buffer, easyUI框架 hessian: Hessian是一个轻量级的remoting on http工具,采用的是Binary RPC协 ...
- maven中文乱码问题——编译错误
新建了个web应用,用maven配置的. Java源代码采用了utf-8编码格式. 本地编译打包,报错误. 由于系统默认编码是GBK,因此需要采用utf-8来编译. 采用如下方式: 在pom中添加 ...
- JAVA和C# 3DES加密解密
最近 一个项目.net 要调用JAVA的WEB SERVICE,数据采用3DES加密,涉及到两种语言3DES一致性的问题, 下面分享一下, 这里的KEY采用Base64编码,便用分发,因为Java的B ...
- 菜鸟学Windows Phone 8开发(1)——创建第一个应用程序
本系列文章来源MSDN的 面向完全新手的 Windows Phone 8 开发 主要是想通过翻译本系列文章来巩固下基础知识顺带学习下英语和练习下自己的毅力(因为打算每天翻译一篇,但是发现翻译这篇花费了 ...