Files
python/TangDou/XiTi/1257_1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

56 lines
1.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
// 基础算法 —— 递归/递推 —— 汉诺塔问题(Hanoi)
// https://blog.csdn.net/u011815404/article/details/80057146
// 递推和递归的区别是什么?
// https://www.zhihu.com/question/20651054
/*
【问题解答】
解:设Hn为n个盘子从a柱移到c柱所需移动的盘次。
显然,当n=1时,只需把a 柱上的盘子直接移动到c柱就可以了,故:H1=1。
当n=2时,先将a柱上面的小盘子移动到b柱上去,然后将大盘子从a柱移到c柱,最后,将b柱上的小盘子移到c柱上,共记3个盘次,故:H2=3。
以此类推,当a柱上有n(n>=2)个盘子时,总是先借助c柱把上面的n-1个盘子移动到b柱上,然后把a柱最下面的盘子移动到c柱上,再借助a柱把b柱上的n-1个盘子移动到c柱上,总共移动H(n-1)+1+H(n-1)个盘次。
∴Hn=2H(n-1)+1,边界条件:H1=1
*/
int ct = 1;//记录步数,在步骤中输出
void move(int n, char from, char to) {
printf("第 %2d 步:把第 %d 个盘子: %c >>>>>>> %c\n", ct++, n, from, to);
}
//输出步数:
int hanoi(int n) {
if (n == 1)
return 1;
else
return 2 * hanoi(n - 1) + 1;
}
//输出步骤
void hanoi_tower(int n, char x, char y, char z) {
if (n == 1)
move(1, x, z);
else {
hanoi_tower(n - 1, x, z, y);
move(n, x, z);
hanoi_tower(n - 1, y, x, z);
}
}
int main() {
int n;//盘子个数
cout << "输入盘子个数:" << endl;
cin >> n;
char x = 'A', y = 'B', z = 'C';
//求步数
int t = hanoi(n);
cout << "一共需要%" << t << "步。";
//开始递归
hanoi_tower(n, x, y, z);
return 0;
}