递归
#include <iostream>
using namespace std;
void hanoi(int N, char source, char relay, char destination) {
if(N == 1)
cout << source << " -> " << destination << endl;
else {
hanoi(N-1, source, destination, relay);
cout << source << " -> " << destination << endl;
hanoi(N-1, relay, source, destination);
}
}
int main() {
int n;
cin >> n;
hanoi(n, 'A' , 'B' , 'C');
return 0;
}
递归汉诺塔问题
最小问题是什么
def moveTower(height,first,second,third):
if height>=1:
moveTower(height-1,first,third,second)
moveDisk(height,first,third)
moveTower(height-1,second,first,third)
def moveDisk(disk,fist,second):
print(f"moving disk[{disk}] from {fist}to{second}")
moveTower(3,"#1","#2","#3")