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);
}
{
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);
}
本文详细介绍了使用递归算法解决经典的汉诺塔问题,包括核心代码实现和步骤解析。
972

被折叠的 条评论
为什么被折叠?



