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. Python 中Editplus 特别实用的设置方法

    editplus 中输入tab自动变成4个空格打开tools->preference打开面板,files的子栏目->settings & syntax面板中的 tab/indent ...

  2. ODAC (V9.5.15) 学习笔记(二十一)数据复制

    用TVirtualTable在内存中缓存TOraQuery中的数据,主要应用场景是参照其他数据,需要将TOraQuery中的数据复制到TVirtualTable,由于没有类似于TClientDataS ...

  3. ux.form.field.SearchField 列表、树形菜单查询扩展

    //支持bind绑定store //列表搜索扩展,支持本地查询 //支持树形菜单本地一级菜单查询 Ext.define('ux.form.field.SearchField', { extend: ' ...

  4. location.hash属性介绍

    location.hash属性介绍 例如URL: http://wwww.a.com/index#rhythmk 通过location.hash 我们将获取到 #rhythmk. 默认浏览器会滚动至i ...

  5. mono+jexus 之连接数据库

    System.ArgumentException Unable to find the requested .Net Framework Data Provider. It may not be in ...

  6. reinstall_xbt: Linux中如何查看文件的最初创建时间

    Linux中如何查看文件的最初创建时间 与文件相关的几个时间: 1.访问时间,读一次这个文件的内容,这个时间就会更新.比如对这个文件使用more命令.ls.stat命令都不会修改文件的访问时间.   ...

  7. xming + putty 搭建远程图形化ssh访问ubuntu 14.04

    putty下载: http://www.putty.org/ 一般我们远程登录linux 服务器,都是使用非加密的 telnet 或者加密的 ssh.这些登录方式有一个特点:只能登录字符界面,不能运行 ...

  8. easyui menubutton combobox 被遮盖问题

    如图一所示,menubutton 中的 combobox 被遮盖 z-Index 不够.这是作者给出的解决方案 <a href="#" class="easyui- ...

  9. 终于找到全annotation配置springMVC的方法了(事务不失效)

    如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将service bean配置到xml文件中才行 这个问题是由于问答上有解决方案 引用 这个问题很经典了 在主容器中 ...

  10. Silver Cow Party(最短路,好题)

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...