The Towers of Hanoi Revisited---(Multi-column Hanoi)

Source: Internet
Author: User
Tags integer numbers

Description

All must know the puzzle named "The Towers of Hanoi". The puzzle have three pegs and N discs of different radii, initially all disks is located on the first peg, ordered by the IR radii-the largest at the bottom and the smallest at the top. In a turn your may take the topmost disc from any peg and move it to another peg, the only rule says that's 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 so somewhere in Tibet there is a monastery where monks tirelessly move disks From peg-to-peg solving the puzzle for discs. The legend says that was they finish, the end of the world would come. Since It is well known so solve the puzzle you need to make 2n -1 moves, a small calculation shows that the WOR LD seems to is a quite safe place for a while.

However, recent archeologists discoveries has shown that the things can be a bit worse. The manuscript found in Tibet Mountains says that the puzzle the monks is solving have not 3 but M pegs. The problem, because when increasing the number of pegs, the number of moves needed to move all discs from the FIR St 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 have M pegs and provi De 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 and other disc.

Disc radii is integer numbers from 1 to N, pegs is numbered from 1 to M.

Sample Input

5 4

Sample Output

13move 1 from 1 to 3move 2 from 1 to 2move 1 from 3 to 2 atop 2move 3 from 1 to 4move 4 from 1 to 3move 3 from 4 to 3 atop  4move 5 from 1 to 4move 3 from 3 to 1move 4 from 3 to 4 atop 5move 3 from 1 to 4 atop 4move 1 from 2 to 1move 2 from 2 to 4 atop 3move 1 from 1 to 4 atop 2

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>using namespacestd;Const intMAXN = 1e2+5;Const DoubleEPS = 1e-9;Const intINF = 1e8+5;intF[MAXN][MAXN], P[MAXN][MAXN];///F: Number of Steps p: nodevoid Get(intNintk) {    if(F[n][k]! =-1)        return; F[N][K]=INF; if(K <3)        return;  for(intm=1; m<n; m++)    {        Get(M, k); Get(N-m, K-1); intTP =2*f[m][k]+f[n-m][k-1]; if(F[n][k] >TP) {F[n][k]=TP; P[N][K]=m; }    }}intN, M;intHANOI[MAXN][MAXN], NUM[MAXN];voidPrintintSintTintAintb) {    if(A = =1) {printf ("move%d from%d to%d", hanoi[s][num[s]]+1, s,t); if(Num[t]) printf ("atop%d", hanoi[t][num[t]]+1); Puts (""); Num[t]++; Hanoi[t][num[t]]=hanoi[s][num[s]--]; return; }     for(intI=1; i<=m; i++)    {        if(I!=s && i!=t) {if(Hanoi[i][num[i]] > hanoi[s][num[s]-p[a][b]+1] {print (S, I, P[a][b], b); Print (S, T, a-P[A][B], b1);                Print (I, T, P[a][b], b); return; }        }    }    return ;}intMain () { while(cin>>n>>m) {memset (F,-1,sizeof(f));  for(intI=1; i<=m; i++) f[1][i] =1; Get(n, m); cout<<f[n][m]<<Endl; Memset (Hanoi,0,sizeof(Hanoi)); memset (num,0,sizeof(num));  for(intI=n; i>=1; i--) {hanoi[1][num[1]] =i; num[1]++; }         for(intI=1; i<=m; i++) hanoi[i][0] =INF; Print (1, M, N, M); }    return 0;}

The Towers of Hanoi Revisited---(Multi-column Hanoi)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.