l540151663 发表于 2013-1-26 12:33:42

汉诺塔算法

void hanoi(int n,char one,char two,char three)         
   
{
      
if(n==1)
   move(n,one,three);//将第n个盘从第1个柱子移到第3个柱子
else
    {
   hanoi(n-1,one,three,two); //将上面的n-1个盘从第1个柱子通过第3个柱子移动到第2个柱子
   move(n,one,three);
   hanoi(n-1,two,one,three);
    }
}

void move(int n,char x,char y)         
{
   printf("%d %c %c\n",n,x,y);
}
页: [1]
查看完整版本: 汉诺塔算法