# CCI题目3-4：Hanoi Tower

By | 2012 年 11 月 20 日

1.将n-1个盘子，从0移动到1，将stack1用作临时stack；
2.将最下面的第n个盘子，从0移动到2；
3.将刚才放到临时stack1的n-1个盘子，移动到stack2。

In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (e.g., each disk sits on top of an even larger one). You have the following constraints:
(A) Only one disk can be moved at a time.
(B) A disk is slid off the top of one rod onto the next rod.
(C) A disk can only be placed on top of a larger disk.
Write a program to move the disks from the first rod to the last using Stacks.

Code

```
//
//  main.cpp
//  CCI.3.4.Hanoi Tower
//
//  Created by Qiu Xiangyu on 12-11-20.
//

#include
#include
using namespace std;

void move(stack stacks[], int count, int sourceIndex, int targetIndex, int tempIndex) {
if (count == 0) {
return;
}
//moving the top of plants to temporary stack, leaving the last one
move(stacks,count - 1,sourceIndex,tempIndex,targetIndex);
//moving the last plate to the target stack
int plate = stacks[sourceIndex].top();
cout<<"moving plate "< stacks[3];
for (int i = 0; i < count; ++i) {
stacks[0].push(count - i);
}
//solve it
move(stacks,count,0,2,1);
}

int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
hanoi(10);
return 0;
}
```