Tower of Hanoi

Topic Description:

  When Genesis, Benares there is a Baltic teach tower, is supported by three diamond rods, at the beginning of God placed 64 small to large from top to bottom according to the arrangement of the gold disk is on the first stick, and ordered the monks All the gold plate moved to a third rod from a first root root stone stone bars, and observe the principle handling process platter under the small plates. If the move is only a daily dish, then when the full move those dishes, this tower will be destroyed, that is, when approaching end of the world.

Algorithms ideas:

  If the column labeled ABC, moved up to the A C, while in only one plate, it will be moved to C, when there are two plates, as it will assist.

  If more than two plates, the plates covered about a third, it is simple.

  Two plates for each treatment, i.e. A-> B, A-> C, B-> C these three steps, the cover portion. It enters a recursive process.

code:

#include <stdio.h>
void hanoi(int n,char A,char B,char C)
{
    if(n==1)
    {
        printf("Move sheet %d from %c to %c\n",n,A,C);
    }
    else
    {
        hanoi(n-1,A,C,B);
        printf("Move sheet %d from %c to %c.",n,A,C);
        hanoi(n-1,B,A,C);
    }
}
int main()
{
    int n;
    printf("请输入盘数:");
    scanf("%d",&n);
    hanoi(n,'A','B','C');
    return 0;
}

operation result:

Reproduced in: https: //my.oschina.net/u/204616/blog/545037

Guess you like

Origin blog.csdn.net/weixin_33756418/article/details/91989994