c 请教汉诺塔问题,请指点

来源:百度知道 编辑:UC知道 时间:2024/07/08 20:15:40
if(n==1) move(a,c);
else
{hanoi(n-1,a,c,b);
move(a,c);
hanoi(n-1,b,a,c);
就是这个地方,虽然知道怎么移动,但是看不懂这个程序呀...急呀谁能写一下它的递归过程,感谢

#include <stdio.h>

void move(char x,char y)
{
printf("%c-->%c\n",x,y);
}
void hanoi (int n,char one ,char two,char three)
{
if(n==1)
move (one ,three);
else
{
hanoi (n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
main()
{
int m;
printf("input the number of diskes:");
scanf("%d",&m);
printf("the step to moving %3d diskes:\n",m);
hanoi(m,'A','B','C');
}
/*运行情况如下:
input the number of diskes:3 回车
the steps to moving 3 diskes:
A-->C
A-->B
C-->B
A-->C
B-->A
B-->C
A-->C

书上说hanoi(n-1,one,three,two);是把“one”上的n-1个往“two”上移,接着move(one,three);
然后是hanoi(n-1,two,one,three)即把“two”上的n-1个往“three”上移;
|