using System;
using System.Collections.Generic;
using System.Text;
    /*
     *
     *   * 经典递归之—汉诺塔!
     *
     */
    class HanNuoTa
    {
        static void Main(string[] args)
        {
            HanNuoTa t = new HanNuoTa();
            //通过实例化对象调用方法,搬10个塔
            t.Method('A', 'B', 'C', 10);
        }
       /*
  •         from,汉诺塔所在初始位置,
  •         end ,最终汉诺塔放置位置,
  •         other 中转位置。
        */
        void Method(char from, char end, char other, int number)
        {
            if (number == 1)
            {
                Console.WriteLine(from + "  >>  " + end);
            }
            else
            {
   // 1.当number大于1时,先把最上面的number-1个塔从from转移  other, 借助于end !
                Method(from,other,end,number-1);
 
 
 
   // 2.再把from上最上面的一个转移到   end!
 
                Method(from, end, other, 1);
   // 3.然后把other 上面number-1个塔从other转移  end, 借助于from!
                Method(other, end, from, number - 1);
            }
        }
 
    }

0

收藏

mayuquan

39篇文章,2W+人气,0粉丝