#include <stdio.h>
int c;
void move(char x, int n, char z)
{
printf("%i. Move disk %i from %c to %c\n",++c, n,x,z);
}
void hanoi(int n, char x, char y, char z)
{
//将塔座x上按直径由小到大编号为1至n的n个圆盘按规则搬到塔座z上。y作为辅助塔座
//扳动操作move(x,n ,z)可定义为
//printf("%i. Move disk %i from %c to %c\n",++c, n,x,z);
if(n == 1)
move(x,1,z);//将编号为1的圆盘从x移到z
else
{
hanoi(n-1, x,z,y);//将x上编号1到n-1的圆盘移动到y,z为辅助塔盘
move(x,n,z);//将编号为n的圆盘从x移到z
hanoi(n-1,y,x,z);//将y上编号为1到n-1的圆盘移动到z,x为辅助塔盘
}
}
int main(void)
{
int n;//n为盘子的数字
scanf("%d",&n);
hanoi(n,'X', 'Y', 'Z');
getchar();
getchar();
return 0;
int c;
void move(char x, int n, char z)
{
printf("%i. Move disk %i from %c to %c\n",++c, n,x,z);
}
void hanoi(int n, char x, char y, char z)
{
//将塔座x上按直径由小到大编号为1至n的n个圆盘按规则搬到塔座z上。y作为辅助塔座
//扳动操作move(x,n ,z)可定义为
//printf("%i. Move disk %i from %c to %c\n",++c, n,x,z);
if(n == 1)
move(x,1,z);//将编号为1的圆盘从x移到z
else
{
hanoi(n-1, x,z,y);//将x上编号1到n-1的圆盘移动到y,z为辅助塔盘
move(x,n,z);//将编号为n的圆盘从x移到z
hanoi(n-1,y,x,z);//将y上编号为1到n-1的圆盘移动到z,x为辅助塔盘
}
}
int main(void)
{
int n;//n为盘子的数字
scanf("%d",&n);
hanoi(n,'X', 'Y', 'Z');
getchar();
getchar();
return 0;
}