汉诺塔算法

伪代码:

hanio(n个盘子,从a搬到c)
{
     if(n==1)
   直接把盘子从a搬到c;
    else
    {
      hanio(n-1个盘子,从a先搬到b);
      最后第n个盘子,从a搬到c;
      hanio(n-1个盘子,从b搬到c);
     }
}

c#

        void Hanoi(int n, char a, char b, char c)
        {
            if(n == 1)
            {
                Console.WriteLine(a + " -> " + c);
            }
            else
            {
                Hanoi(n - 1, a, c, b);
                Console.WriteLine(a + " -> " + c);
                Hanoi(n - 1, b, a, c);
            }
        }

//调用示例:Hanoi(4, 'A', 'B', 'C');

你可能感兴趣的:(汉诺塔算法)