《CTCI》3.4 汉诺塔游戏

本文介绍了一种经典的递归问题——汉诺塔,并提供了两种不同的解决方案:递归方法和迭代方法。通过这两种方法,文章详细展示了如何使用栈来实现盘子从一个柱子移动到另一个柱子的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

《CTCI》 P140

3 栈与队列

题目

3.4 In the classic problem of the Towers of Hanoi, you have 3 towers 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 (i.e., each disk sits on top of an even larger one). You have the following constraints:
(1) Only one disk can be moved at a time.
(2) A disk is slid off the top of one tower onto the next tower.
(3) A disk can only be placed on top of a larger disk.
Write a program to move the disks from the first tower to the last using stacks.

解答

//3.4
//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 :
//
//Only one disk can be moved at a time.
//A disk is slid off the top of one rod onto the next rod.
//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


#include <iostream>
#include <stack>
#include <windows.h>

using namespace std;

//递归方法
//将n个碟子从src经过buf搬到dst
void moveDisks(int n, char src, char buf, char dst, int &step)
{
    if (n == 1)
    {
        cout << step++ << ":\t" << 0 << " 从 " << src << " 到 " << dst << endl;
        return;
    }
    moveDisks(n - 1, src, dst, buf, step);
    cout << step++ << ":\t" << n - 1 << " 从 " << src << " 到 " << dst << endl;
    moveDisks(n - 1, buf, src, dst, step);
}


struct op
{
    int begin, end;
    char src, buf, dst;
    op(){}
    op(int pbegin, int pend, int psrc, int pbuf, int pdst) :begin(pbegin), end(pend), src(psrc), buf(pbuf), dst(pdst){}
};


//迭代方法
void hanoi(int n, char src, char buf, char dst)
{
    stack<op> st;
    op tmp;
    st.push(op(0, n - 1, src, buf, dst));
    int step = 0;

    while (!st.empty())
    {
        tmp = st.top();
        st.pop();
        if (tmp.begin != tmp.end)
        {
            st.push(op(tmp.begin, tmp.end - 1, tmp.buf, tmp.src, tmp.dst));
            st.push(op(tmp.end, tmp.end, tmp.src, tmp.buf, tmp.dst));
            st.push(op(tmp.begin, tmp.end - 1, tmp.src, tmp.dst, tmp.buf));
        }
        else
        {
            cout << step++ << ":\t" << "Move disk " << tmp.begin << " from " << tmp.src << " to " << tmp.dst << endl;
        }
    }
    cout << "total: " << step << " = 2 ^ " << n << " - 1" << " steps" << endl;
}

int main()
{
    cout << "汉 诺 塔 游 戏" << endl;
    int n = 5;
    int step = 0;
    cout << "总碟数: " << n << endl;
    cout << "步骤:" << endl;
    moveDisks(n, 'A', 'B', 'C', step);
    cout << "共 " << step << " = 2 ^ " << n << " - 1" << " 步" << endl;
    cout << endl;
    hanoi(n, 'A', 'B', 'C');

    system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值