You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
735 B
24 lines
735 B
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
// 递归实现汉诺塔
|
|
// https://www.zhihu.com/question/24385418
|
|
void move(int n, char a, char b, char c) {
|
|
if (n == 0) return;
|
|
if (n == 1) {
|
|
cout << a << "->" << n << "->" << b << endl;
|
|
} else {
|
|
move(n - 1, a, c, b); //理解 三种柱子 其中有一个辅助柱子 将n-1个盘子 由a移动到c 其中b为辅助柱子
|
|
cout << a << "->" << n << "->" << b << endl; //然后将n个柱子 由a 移到 b即可
|
|
move(n - 1, c, b, a); //剩下的 再将n-1个盘子 由c移动到b 其中a为辅助柱子
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
int n;
|
|
char a, b, c;
|
|
cin >> n >> a >> b >> c;
|
|
move(n, a, b, c);
|
|
return 0;
|
|
} |