【问题标题】:Tower of Hanoi C++(using recursion)河内塔 C++(使用递归)
【发布时间】:2013-08-28 21:28:24
【问题描述】:

我编写了以下代码作为练习。
我在打印目标堆栈时得到不正确的输出。
谁能指出我哪里出错了?

//Tower of Hanoi using Stacks!
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class Stack
{
private:
    int *t;
    int length, top;

public:
    Stack(int len)
    {
        length=len;
        t= new int[len];
        top=-1;
    }

    ~Stack()
    {
        delete []t;
    }

    void push(int d)
    {
        top++;
        t[top]=d;
    }

    int pop()
    {
        top--;
        return t[top+1];
    }

    void printstack()
    {
        int cur=top;
        while(cur>-1)
        {
            cout<<t[cur]<<endl;
            cur--;
        }
    }
};

void MoveTowerofHanoi(int disk, Stack *source, Stack *temp, Stack *destination)
{
    if (disk==0)
    {
        destination->push(source->pop());
    }
    else
    {
        MoveTowerofHanoi(disk-1,source,temp,destination);
        destination->push(source->pop());
        MoveTowerofHanoi(disk-1,temp,destination,source);
    }
}

void main()
{
    clrscr();
    int disks;
    cout<<"Enter the number of disks!"<<endl;
    cin>>disks;
    Stack* source=new Stack(disks);
    for(int i=0; i<disks; i++) {
        source->push(disks-i);
    }
    cout<<"Printing Source!"<<endl;
    source->printstack();
    Stack* temp=new Stack(disks);
    Stack* destination=new Stack(disks);
    MoveTowerofHanoi(disks,source,temp,destination);
    cout<<"Printing Destination!"<<endl;
    destination->printstack();
    getch();
}

这是我得到的输出:

Enter the no. of disks!  
3  
Printing Source!  
1  
2  
3  
Printing Destination!  
-4

编辑后代码如下:

    void MoveTowerofHanoi(int disk, Stack *source, Stack *destination, Stack *temp)
{
    if (disk==1)
    {
        destination->push(source->pop());
    }
    else
    {
        MoveTowerofHanoi(disk-1,source,temp,destination);
        destination->push(source->pop());
        MoveTowerofHanoi(disk-1,temp,destination,source);
    }
}

第一个错误是:

void MoveTowerofHanoi(int disk, Stack *source, Stack *temp, Stack *destination)

第二个是:

if (disk==0)

非常感谢大家的帮助!


对堆栈类所做的更改:

void push(int d)
{
     if(top<length-1)
    {
    top++;
    t[top]=d;
    }

}

int pop()
{
    if(top>-1)
    {
    top--;
    return t[top+1];
    }
}

【问题讨论】:

  • main 中肯定不需要任何指针,它必须返回int,而不是void,如果指向的是河内塔,为什么不能直接使用std::stack?此外,iostream.h 不是,也从来不是标准标头。
  • 您在类定义后缺少一个分号,它甚至不应该编译。而你的缩进,嗯,你没有缩进,这使得代码很难阅读。
  • 买一本现代的 C++ 书,你的书很糟糕。该代码包含许多错误,现代编译器将视为无效而将其拒绝。
  • 尝试在调试器中运行,并逐行遍历递归调用,看看会发生什么。
  • @IshaanSharma stackoverflow.com/questions/388242/… 看这里的书籍推荐

标签: c++ recursion stack towers-of-hanoi


【解决方案1】:

这行得通:

//Tower of Hanoi using Stacks!
#include<iostream>
//#include<conio.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
using namespace std;

void print_elem(int elem)
{
    cout << elem << endl;       
}

class Stack{
public:
    void push(int d){t.push_back(d);}
    int pop()
    {
        int d=t.back();
        t.pop_back();
        return d;
    }
    void printstack()
    {
        for_each(t.rbegin(),t.rend(),print_elem);
    }
private:
    vector<int> t;

};

void MoveTowerofHanoi(int disk, Stack *source, Stack *temp, Stack *destination)
{
    if (disk==1)
    {
        destination->push(source->pop());
    }
    else
    {
        MoveTowerofHanoi(disk-1,source,destination,temp);
        destination->push(source->pop());
        MoveTowerofHanoi(disk-1,temp,source,destination);
    }
}

int main()
{
    int disks;
    cout<<"Enter the number of disks!"<<endl;
    cin>>disks;
    Stack* source = new Stack();
    for(int i=disks; i>0; --i) {
        source->push(i);
    }

    cout<<"Printing Source!"<<endl;
    source->printstack();
    Stack* temp = new Stack();
    Stack* destination = new Stack();
    MoveTowerofHanoi(disks,source,temp,destination);
    cout<<"Printing Destination!"<<endl;
    destination->printstack();
    delete source;
    delete temp;
    delete destination;
}

【讨论】:

  • @NeilKirk,thanx,已修复。
  • 请注意,对于河内塔,您永远不能将较大的磁盘放在任何堆栈上的较小磁盘的顶部。因此,ToH 的堆栈类可以强制执行此约束,从而为算法提供更严格的测试。堆栈类还可以确保只有值 1..length 被放置在堆栈上——同样,这是一个特定于 ToH 问题的约束。当然,当算法正确时,额外的检查并没有多大帮助,但当它被破坏时,它会很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-04
  • 2016-01-27
  • 1970-01-01
相关资源
最近更新 更多