编写汉诺塔程序c++

编写汉诺塔程序,加入打印递归过程,观察递归运算的机里。即递归进入的时候,打印这
是第几层,从递归退出时,打印递归深度。 c++

#include <iostream>
using namespace std;
#include <cmath>


void visit(int n, char x, char y)
{
    static int count = 1;
    //printf("%d:把编号%d从%c搬运到%c\n", count++, n, x, y);
}

void Hanoi(int n, char a, char b, char c, int depth)//实则真正操作的是a和c
{
    cout << depth << endl;
    if (n == 1)
    {
        visit(1, a, c);
        return;
    }


    Hanoi(n - 1, a, c, b, depth + 1);//首先将a搬运至b,注意第一个和第三个操作数。
    visit(n, a, c);//将编号n的圆盘从A搬运到C
    Hanoi(n - 1, b, a, c, depth + 1);//接着将b搬运至c
    cout << depth << endl;
}

int main()
{
    int n = 3;
    cout << "需要搬运的次数:" << pow(2, n) - 1 << endl;//搬运次数:2^n - 1
    Hanoi(n,'A', 'B', 'C', 0);
    
}