#include <stdio.h>
#include <stdlib.h>
void hanoi(int, char, char, char);
void move(int, char, char);
int main(void)
{
hanoi(3, 'A', 'B', 'C');
return 0;
}
//将n个塔座从a搬到c,b为辅助
void hanoi(int n, char a, char b, char c)
{
if (n > 0)
{
//先将n - 1个塔座从a搬到b,c为辅助
hanoi(n - 1, a, c, b);
//将第n个从a搬到c
move(n, a, c);
//再将n - 1个塔座从b搬到c,a为辅助
hanoi(n - 1, b, a, c);
}
}
void move(int n, char from, char to)
{
printf("move %d from %c to %c\n", n, from, to);
}
汉诺塔问题
最新推荐文章于 2020-02-12 19:47:03 发布