有A,B,C三根柱子,其中A柱子有32个圆盘,且在相邻的两个圆盘中下面的圆盘比上面的大,先要求把A柱子上所有圆盘移动到C柱上,在移动的过程中不允许大的圆盘压在小的圆盘上
递归:在谭浩强的《C语言程序设计》和严蔚敏的《数据结构》上都有详细介绍。
1、将上面n-1个P 通过C从A移动到B
2、将第 n 个P 从A移动到C
3、将n-1个P 通过A从B移动到C
int hanoi(char a, char b, char c, int n){
if(n==1){
move(a, c);
return 1;
}
hanoi(a, c, b, n-1);
move(a, c);
}
一楼的,少一个move函数。
楼主自己具体化吧!
void move(char place, char to)
{
printf ("%c - > %c \n", place, to);
}
void hannoi(unsigned N, char from, char to, char buff)
{
if(N==1)
move(place,to);
else{
hannoi(N-1, place, buff, to);
move(place, to);
hannoi(N-1, buff, to, place);
}
}