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

      Input file contains N and M (1 ≤ N ≤ 64, 4 ≤ M ≤ 65).

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---(多柱汉诺塔)的更多相关文章

  1. 4柱汉诺塔(zz)

    多柱汉诺塔可以用Frame–Stewart算法来解决. The Frame–Stewart algorithm, giving a presumably optimal solution for fo ...

  2. 多柱汉诺塔问题“通解”——c++

    多柱汉诺塔问题 绪言 有位同学看到了我的初赛模拟卷上有一道关于汉诺塔的数学题.大概就是要求4柱20盘的最小移动次数. 他的数学很不错,找到了应该怎样推. 如果要把n个盘子移到另一个柱子上,步骤如下: ...

  3. hdu 1207 四柱汉诺塔

    递推,汉诺塔I的变形. 这题真心没想到正确解法,越想越迷糊.这题看了别人题解过得,以后还是自己多想想,脚步太快并非好事. 贴上分析:   分析:设F[n]为所求的最小步数,显然,当n=1时,F[n]= ...

  4. SGU 202. The Towers of Hanoi Revisited

    多柱汉诺塔问题. 引用自wiki百科 多塔汉诺塔问题 在有3个柱子时,所需步数的公式较简单,但对于4个以上柱子的汉诺塔尚未得到通用公式,但有一递归公式(未得到证明,但目前为止没有找到反例): 令为在有 ...

  5. 四柱加强版汉诺塔HanoiTower----是甜蜜还是烦恼

    我想很多人第一次学习递归的时候,老师或者书本上可能会举汉诺塔的例子. 但是今天,我们讨论的重点不是简单的汉诺塔算法,而是三柱汉诺塔的延伸.先来看看经典的三柱汉诺塔. 一.三柱汉诺塔(Hanoi_Thr ...

  6. 汉诺塔的问题:4个柱子,如果塔的个数变位a,b,c,d四个,现要将n个圆盘从a全部移到d,移动规则不变

    四柱汉诺塔问题的求解程序.解题思路:如a,b,c,d四柱. 要把a柱第n个盘移到目标柱子(d柱),先把上层 分两为两部份,上半部份移到b柱,下半部分移到c柱,再把第n盘移到 目标柱子,然后,c柱盘子再 ...

  7. HDU汉诺塔系列

    这几天刷了杭电的汉诺塔一套,来写写题解. HDU1207 汉诺塔II HDU1995 汉诺塔V HDU1996 汉诺塔VI HDU1997 汉诺塔VII HDU2064 汉诺塔III HDU2077  ...

  8. [递推]B. 【例题2】奇怪汉诺塔

    B . [ 例 题 2 ] 奇 怪 汉 诺 塔 B. [例题2]奇怪汉诺塔 B.[例题2]奇怪汉诺塔 题目描述 汉诺塔问题,条件如下: 这里有 A A A. B B B. C C C 和 D D D ...

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

随机推荐

  1. 云计算的三层SPI模型

    (转自:http://hi.baidu.com/fengjun8216/item/b15bbef4dcf74049922af27b) 一般而言,云计算架构可以用三层SPI模型来表述. 一.基础设施即服 ...

  2. ALTER SEQUENCE 修改序列解决唯一约束冲突 unique constraint violated

    背景 自增序列会遇到也会遇到唯一约束冲突吗?是的,最常见的情况就是数据迁移之后,导致数据最大值超过序列值. 软件开发中不遇到些出乎意料的问题,总感觉不太够劲. 修改序列(ALTER SEQUENCE) ...

  3. square开源vim,tmux配置在linux上使用

    首先安装需要的软件 apt-get install vim ack-grep git tmux gnome-terminal ctags xclip silversearcher-ag 这里tmux需 ...

  4. (转)使用Custom Draw实现ListCtrl的重绘

    使用Custom Draw实现ListCtrl的重绘   common control 4.7版本介绍了一个新的特性叫做Custom Draw,这个名字显得模糊不清,让人有点摸不着头脑,而且MSDN里 ...

  5. (转)关于tcp和udp的缓冲区

    (一)基础知识 IPv4 数据报最大大小是65535(16位),包括IPv4头部. IPv6 数据报最大大小是65575,包括40个字节的IPv4头部 MTU,这是由硬件规定的,如以太网的MTU是15 ...

  6. Oracle 物化视图 说明

    一.    物化视图概述 Oracle的物化视图是包括一个查询结果的数据库对像,它是远程数据的的本地副本,或者用来生成基于数据表求和的汇总表.物化视图存储基于远程表的数据,也可以称为快照. 物化视图可 ...

  7. 没有找到cxcore100.dll,因此这个应用程序未能启动,重新安装应用程序可能会修复此问题

    第一种情况: 出现这个问题多数是因为“环境变量PATH”未设置,虽然你可能在安装的过程中勾选了Add <...>\OpenCV\bin to the system PATH项!安装Open ...

  8. [转]Visual Studio技巧之打造拥有自己标识的代码模板

    可能经过很多博客的介绍,大家都知道代码段的使用,使用代码段可以很方便地生成一些常用的代码格式,确实对我们开发很方便.在团队开发中或者在某些情况下我们经常可能还会希望使用Visual Studio生成的 ...

  9. 扩展 IEnumerable<T>,让它根据另一个集合的顺序来排列

    假如我有两个集合: public class Teacher { public int Id { get; set; } public string Name { get; set; } } publ ...

  10. 技术总结之SpringIOC

    1)SpringIOC核心模拟实现 思路:初始化Spring容器时,从配置文件中读取定义好的Bean的信息,根据配置属性初始化后存入Spring容器中. 当需要某个Bean时,直接从容器中通过id获取 ...