求解思想:大而化小
1、问题拆分成子问题
2、对子问题求解
在汉诺塔游戏中,有三个分别命名为A、B、C得塔座,几个大小各不相同,从小到大一次编号得圆盘,每个原盘中间有一个小孔。最初,所有得圆盘都在A塔座上,其中最大得圆盘在最下面,然后是第二大,以此类推.
先上代码
#include<iostream>
using namespace std;
class Move
{
public:
void moveGo(int i, int a, int b, int c);
void display(int i, int a, int b);
private:
static int count;
};
int Move::count = 1;
void Move::moveGo(int i, int a, int b, int c)
{
if (1 == i)
{
display(1, a, b);
}
else
{
moveGo(i - 1, a, c, b);
display(i, a, b);
moveGo(i - 1, c, b, a);
}
}
void Move::display(int i, int a, int b)
{
printf("第 %d 步:移动第 %d 个塔从 %d 根柱子到 %d 根柱子\n", count,i, a, b);
count++;
}
int main()
{
Move mo;
mo.moveGo(4,1,2,3);
cout << endl;
system("pau