#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 lr diskes:");
scanf("d",&m);
printf("the step to moving %3d diskes:\n",m);
hanoi(m,'A','B','C');
}
这是汉诺塔问题。请问有谁能解释一下hanoi函数的具体递归过程吗?我怎么总是看不明白。
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);
}
}
这中间到底是如何实现的呢?
即hanoi (n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);