A B C
当只有一个盘子的时候,只移动一次
当盘子大于一时,先将n-1个盘子通过C移动到B,再将剩下的移动到C,再将n-1个盘子通过 A移动到C.
#include <stdio.h>
void move(char pos1, char pos2)
{
printf(" %c -> %c ", pos1, pos2);
}
void hanoi(int n, char pos1, char pos2, char pos3)
{
if (n == 1)
{
move(pos1, pos3);
}
else
{
hanoi(n - 1, pos1, pos3, pos2);
move(pos1, pos3);
hanoi(n - 1, pos2, pos1, pos3);
}
}
int main()
{
hanoi(1, 'A', 'B', 'C');
printf("\n");
hanoi(2, 'A', 'B', 'C');
printf("\n");
hanoi(3, 'A', 'B', 'C');
return 0;
}