欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

数据结构Review——栈与队列练习(汉诺塔)

程序员文章站 2024-03-16 16:00:52
...
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

void solution();

void hanoi(int n, char src, char tar, char ass);
void move(char src, char tar);

int main()
{
    int T = 0;
    int t = 0;

    T = 1;
    while (t < T)
    {
        solution();
        t++;
    }

    system("pause");
    return 0;
}

void solution()
{
    int n = 10;
    char src = 'A';
    char tar = 'B';
    char ass = 'C';
    hanoi(n, src, ass, tar);
}

void hanoi(int n, char src, char tar, char ass)
{
    if (n == 0)
    {
        return;
    }
    hanoi(n - 1, src, ass, tar);
    move(src, tar);
    hanoi(n - 1, ass, tar, src);
}

void move(char src, char tar)
{
    cout << src << " -> " << tar << endl;
}