#include "pch.h"
#include <iostream>
using namespace std;
void move(char one, char three) {
cout << one << "->" << three << endl;
}
//n为汉诺塔层数,one为第一根柱子(起始柱子),two第二根柱子(辅助),three第三根柱子(目标柱子)
void hanoi(int n, char one, char two, char three) {
if (n == 1) { //如果只有一层,从第一根柱子直接移到第三根柱子
move(one,three);
}
else { //将前n-1层看作一层,
hanoi(n - 1, one, three, two); //将前n-1层移到第二根柱子(辅助),第二根柱子不总是中间那根
move(one,three); //最后一层移到目标柱子
hanoi(n - 1, two, one, three); //前n-1层移到目标柱子
}
}
int main()
{
int n;
cout << "请输入汉诺塔层数" << endl;
cin >> n;
cout << n << "层汉诺塔的解为:" << endl;
hanoi(n, 'a', 'b', 'c');
}