算法篇-用栈来求解汉诺塔问题

算法篇-用栈来求解汉诺塔问题

阅读了java版的《程序员代码面试指南 IT名企算法与数据结构题目最优解》后,用c++以自己的想法完成了这道题

完整题目

在汉诺塔规则的基础上,限制不能从最左的塔移动到最右的塔上,必须经过中间的塔,移动的跨度只能是一个塔。当塔有N层的时候,打印最优移动过程和最优移动步数。
看到原题我实际上并不能理解这道题,百度后才理解
相传在古印度圣庙中,有一种被称为汉诺塔(Hanoi)的游戏。该游戏是在一块铜板装置上,有三根杆(编号A、B、C),在A杆自下而上、由大到小按顺序放置64个金盘(如下图)。游戏的目标:把A杆上的金盘全部移到C杆上,并仍保持原有顺序叠好。操作规则:每次只能移动一个盘子,并且在移动过程中三根杆上都始终保持大盘在下,小盘在上,操作过程中盘子可以置于A、B、C任一杆上。

题目限制:

1.一次只能移动一个数字
2.大的数字不能出现在小的盘子(数字)的上方(栈顶)
3.左右两栈盘子(数字)移动必须经过中间的塔

递归实现
递归实现基本依照书本实现思路

int minstep;//步数
//递归

void move(int num, string from, string to)
{
    if (num == 1)
    {
        if (from=="mid" || to == "mid")
        {
            cout << "Move " << num << " from " << from << " to " << to << endl;
            minstep+=1;
        }
        else {
            cout << "Move " << num << " from " << from << " to " << "mid" << endl;
            cout << "Move " << num << " from " << "mid" << " to " << to << endl;
            minstep += 2;
        }
    }
    else {
        if (from=="mid" || to == "mid")
        {
            string another = (from._Equal("left") || to._Equal("left")) ? "right" : "left";
            move(num - 1, from, another);
            cout << "Move " << num << " from " << from << " to " << to << endl;
            minstep += 1; 
            move(num - 1, another, to);
        }
        else {
            move(num-1, from, to);
            cout << "Move " << num << " from " << from << " to " << "mid" << endl;
            minstep += 1;
            move(num - 1, to, from);
            cout << "Move " << num << " from " << "mid" << " to " << to << endl;
            minstep += 1;
            move(num - 1, from, to);

        }
    }



}

 void HanoiProblemnBydfd(int num)
{
    if (num <= 0) return;
    minstep  = 0; 

    move(num, "left", "right");
 }

测试

int main()
{
    HanoiProblemnBydfd(3);
    cout << minstep << endl;

    return 0;
}

栈模拟
栈模拟实现和书本略有不同,由于每次移动只能出现一种方式,所以把所有盘子的移动判断完全由程序判断

#include <stack>
#include <iostream>
#include <string>
using namespace std;
long minstep = 0;//最小步数
//非递归,用栈模拟
class HanoiStack {
private:
    stack<int> LeftP;
    stack<int> MidP;
    stack<int> RightP;
    int n;
    string lastfrom;
    string lastto;
    void Move(string from,string to)
    {
        if (from== lastto&&to== lastfrom) return;

        int fromVal=0;
        if (from == "left"&&!LeftP.empty())
        {
            fromVal = LeftP.top();
            //LeftP.pop();
        }
        else if (from == "mid" && !MidP.empty())
        {
            fromVal = MidP.top();
            //MidP.pop();
        }
        else if (from == "right" && !RightP.empty())
        {
            fromVal = RightP.top();
            //RightP.pop();
        }
        if (fromVal == 0) return;
        if (to == "left")
        {
            if(LeftP.empty()) LeftP.push(fromVal);
            else if(fromVal<LeftP.top()) LeftP.push(fromVal);
            else return;
        }
        else if(to=="mid") 
        {
            if (MidP.empty()) MidP.push(fromVal);
            else if (fromVal<MidP.top()) MidP.push(fromVal);
            else return;
        }
        else if (to == "right")
        {
            if (RightP.empty()) RightP.push(fromVal);
            else if (fromVal<RightP.top()) RightP.push(fromVal); 
            else return;
        }
        Pop(from);
        lastto = to;
        lastfrom = from;
        cout << "Move " << fromVal << " from " << from << " to " << to << endl;
        minstep += 1;
    }
    void Pop(string from)
    {
        if (from == "left" && !LeftP.empty())
        {

            LeftP.pop();
        }
        else if (from == "mid" && !MidP.empty())
        {

            MidP.pop();
        }
        else if (from == "right" && !RightP.empty())
        {

            RightP.pop();
        }
    }
public:
    HanoiStack(int n)
    {
        for (int i = n; i >0; i--)
        {
            LeftP.push(i);
        }
        this->n = n;
        minstep = 0;
    }
    void HanoiProblem()
    {
        while (RightP.size() != n)
        {
            Move("left", "mid");
            Move("mid", "left");
            Move("mid", "right");           
            Move("right", "mid");               
        }
        cout << minstep << endl;
    }

};

测试

int main()
{
    /*HanoiProblemnBydfd(3);
    cout << minstep << endl;*/
    HanoiStack  test = HanoiStack(3);
    test.HanoiProblem();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值