zoukankan      html  css  js  c++  java
  • 汉诺塔-sdut

    汉诺塔

    Time Limit: 1000MS Memory Limit: 65536KB

    Problem Description

    汉诺塔(又称河内塔)问题是印度的一个古老的传说。

    开天辟地的神勃拉玛在一个庙里留下了三根金刚石的棒A、B和C,A上面套着n个圆的金片,最大的一个在底下,其余一个比一个小,依次叠上去,庙里的众僧不倦地把它们一个个地从A棒搬到C棒上,规定可利用中间的一根B棒作为帮助,但每次只能搬一个,而且大的不能放在小的上面。

    僧侣们搬得汗流满面,可惜当n很大时这辈子恐怕就很搬完了。

    聪明的你还有计算机帮你完成,你能写一个程序帮助僧侣们完成这辈子的夙愿吗?

    Input

    输入金片的个数n。这里的n<=10。

    Output

    输出搬动金片的全过程。格式见样例。

    Example Input

    2

    Example Output

    Move disk 1 from A to B
    Move disk 2 from A to C
    Move disk 1 from B to C

    Hint

    可以用递归算法实现。

    Author

    代码示例
     
     
    #include <stdio.h>
    void hanoi(int n, char one, char two, char three);
    void move(int n, char getone, char putone);
    int main()
    {
        int n;
        scanf("%d",&n);
        hanoi(n, 'A', 'B', 'C');
        return 0;
    }
    
    void hanoi(int n, char one, char two, char three)
    {
        if(n==1)
        {
            move(n, one, three);
        }
        else
        {
            hanoi(n-1, one, three, two);
            move(n, one, three);
            hanoi(n-1,two, one, three);
        }
    }
    void move(int n, char getone, char putone)
    {
        printf("Move disk %d from %c to %c
    ",n,getone, putone);
    }
    
    
  • 相关阅读:
    codeforces 1215 E Marbles-----状压DP
    留坑待填
    Catalan数
    砝码称重
    约数和
    硬币题解
    迎春舞会之数字舞蹈
    过剩数
    猜测棋局
    [NOIP普及组2014第三题]螺旋矩阵
  • 原文地址:https://www.cnblogs.com/lxhax/p/6673363.html
Copyright © 2011-2022 走看看