#include <stdio.h> int move(int n,int from, int to,int through); int count=0; int main() { move(3,'A','C','B'); return 0; } int move(int n,int from, int to,int through){ if(n==1){ count++; return printf("#%d move %d from %c to %c through %c ",count,n,from,to,through); } else{ move(n-1,from,through,to); count++; printf("#%d move %d from %c to %c through %c ",count,n,from,to,through); move(n-=1,through,to,from); } }
https://www.bilibili.com/read/cv3361466/转载
二进制与汉诺塔:(非严格形式)
-
盘子从小到大对应二进制位数从小到大;
- 每次移动规则:盘子从小到大移动,路径从起始柱到终点柱,盘子每次移动最大柱数(可越过终止柱&&不能重复循环计数),终止柱的下一根柱子为起始柱;
- 每个盘子移动对应一次二进制计数;
- 有n个盘子,至少要移动2^n-1次;