The Towers of Hanoi Revisited---(多柱漢諾塔)

來源:互聯網
上載者:User

標籤:

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

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 namespace std;const int MAXN = 1e2+5;const double eps = 1e-9;const int INF = 1e8+5;int f[MAXN][MAXN], p[MAXN][MAXN];///f:步數 p:節點void get(int n, int k){    if(f[n][k] != -1)        return;    f[n][k] = INF;    if(k < 3)        return;    for(int m=1; m<n; m++)    {        get(m, k);        get(n-m, k-1);        int tp = 2*f[m][k]+f[n-m][k-1];        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 == 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(int i=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], b-1);                print(i, t, p[a][b], b);                return;            }        }    }    return ;}int main(){    while(cin>>n>>m)    {        memset(f, -1, sizeof(f));        for(int i=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(int i=n; i>=1; i--)        {            hanoi[1][num[1]] = i;            num[1]++;        }        for(int i=1; i<=m; i++)            hanoi[i][0] = INF;        print(1, m, n, m);    }    return 0;}

 

The Towers of Hanoi Revisited---(多柱漢諾塔)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.