有三根杆子A,B,C。A杆上有N个(N>1)穿孔圆环,盘的尺寸由下到上依次变小。要求按下列规则将所有圆盘移至C杆:每次只能移动一个圆盘;大盘不能叠在小盘上面。如何移?最少要移动多少次?
原理可参考: 中的讲解
#includeusing namespace std;void Hanoi(int,char,char,char);int main(){ int n; cin >> n; Hanoi(n, 'a', 'b', 'c'); system("pause"); return 0;}void Hanoi(int n, char a, char b, char c) //move a's plates to c column{ if (n == 1) { cout << "move plate " << n << " from " << a << " to " << c << endl; //move the last plate to the target column } else { Hanoi(n - 1, a, c, b); //move (n-1)*plate from a(previous column) to b(transition column) cout << "move plate " << n << " from " << a << " to " << c << endl;//move 'n' plate from a to target column Hanoi(n - 1, b, a, c); //move (n-1)*plate from b(transition column) to c(target column) }}