简体   繁体   English

如何制作硬编码堆栈,而不是硬编码,并保持程序正常运行

[英]How can I make hardcoded stacks, not hardcoded and keep the program running properly

The Towers of Hanoi is very interesting and hard problem, which I assume we all know, but if someone does not here it is: 河内塔楼是一个非常有趣且棘手的问题,我想大家都知道,但是如果有人不在这里,那就是:

Towers of Hanoi problem 河内问题塔

I have been searching and trying to implement a complete C++ solution, where by "complete" I mean that for a given number of disks the program calculates the steps needed and prints out every step and current state of every rod. 我一直在寻找并尝试实现一个完整的C ++解决方案,其中“完成”是指对于给定数量的磁盘,该程序将计算所需的步骤,并打印出每个步骤以及每个杆的当前状态。 Where the rods are represented as stacks. 杆表示为堆栈。

For example let's take 3 disks. 例如,让我们拿3个磁盘。 The output in this case must be: 在这种情况下,输出必须是:

Enter number of disks: 3
Source: 3, 2, 1
Destination: empty rod
Spare: empty rod
----------------------------
Step #1: Moved disk 1
Source: 3, 2
Destination: 1
Spare: empty rod
----------------------------
Step #2: Moved disk 2
Source: 3
Destination: 1
Spare: 2
----------------------------
Step #3: Moved disk 1
Source: 3
Destination: empty rod
Spare: 2, 1
----------------------------
Step #4: Moved disk 3
Source: empty rod
Destination: 3
Spare: 2, 1
----------------------------
Step #5: Moved disk 1
Source: 1
Destination: 3
Spare: 2
----------------------------
Step #6: Moved disk 2
Source: 1
Destination: 3, 2
Spare: empty rod
----------------------------
Step #7: Moved disk 1
Source: empty rod
Destination: 3, 2, 1
Spare: empty rod
----------------------------

I'm having a very BIG trouble and headache when I try to do that with C++. 当我尝试使用C ++进行操作时,我遇到了很大的麻烦和头痛。 I start with making the stacks hardcoded just to test the problem and attain the following: 我首先对堆栈进行硬编码,以测试问题并达到以下目标:

#include <iostream>
#include <stack>

std::stack<int> src;
std::stack<int> dest;
std::stack<int> spare;

int setTowers()
{
    int disks;
    std::cout << "Enter number of disks: ";
    std::cin >> disks;

    for (int i = 0; i < disks; i++) src.push(disks - i);
    return disks;
}

void printRod(std::stack<int>tower)
{
    std::stack<int>temp;
    while (!tower.empty())
    {
        temp.push(tower.top());
        tower.pop();
    }
    if (!temp.size()) std::cout << "empty rod\n";
    else
    {
        while (!temp.empty())
        {
            std::cout << temp.top();
            temp.pop();
            if (temp.size() != 0) std::cout << ", ";
        }
        std::cout << '\n';
    }
}

void PrintRods()
{
    std::cout << "Source: "; printRod(src);
    std::cout << "Destination: ", printRod(dest);
    std::cout << "Spare: ", printRod(spare);
    std::cout << "----------------------------\n";
}

void MoveDisks(int disks, std::stack<int>& src, std::stack<int>& dest, std::stack<int>& spare,unsigned& stepsTaken)
{
    if (disks < 1)  return; 
    else if (disks == 1)
    {
        stepsTaken++;
        dest.push(src.top());
        src.pop();
        std::cout << "Step #" << stepsTaken << ": Moved disk " << disks << '\n';
        PrintRods();
        return;
    }
    else
    {
        MoveDisks(disks - 1, src, spare, dest, stepsTaken);
        stepsTaken++;
        dest.push(src.top());
        src.pop();
        std::cout << "Step #" << stepsTaken << ": Moved disk " << disks << '\n';
        PrintRods();
        MoveDisks(disks - 1, spare, dest, src, stepsTaken);
    }
}
int main()
{
    unsigned stepsTaken(0);
    int disks = setTowers();
    PrintRods();
    MoveDisks(disks, src, dest, spare, stepsTaken);
    return 0;
}

which works just fine, BUT when I try to make the stacks not hard coded the result collapses. 效果很好,但是当我尝试不对堆栈进行硬编码时,结果崩溃了。 It gives wrong result like: 它给出错误的结果,例如:

Enter number of disks: 3
Source: 3, 2, 1
Destination: empty rod
Spare: empty rod
----------------------------
Step #1: Moved disk 1
Source: 3, 2
Destination: 1
Spare: empty rod
----------------------------
Step #2: Moved disk 2
Source: 3
Destination: 1
Spare: 2
----------------------------
Step #3: Moved disk 1
Source: empty rod
Destination: 2, 1
Spare: 3
----------------------------
Step #4: Moved disk 3
Source: empty rod
Destination: 2, 1
Spare: 3
----------------------------
Step #5: Moved disk 1
Source: 2
Destination: 1
Spare: 3
----------------------------
Step #6: Moved disk 2
Source: empty rod
Destination: 1
Spare: 3, 2
----------------------------
Step #7: Moved disk 1
Source: empty rod
Destination: 3, 2, 1
Spare: empty rod
----------------------------

or even worse when I try to fix it more deeply. 甚至当我尝试更深入地修复它时,甚至更糟。

Can someone PLEASE help me how to move the stacks inside the main function? 有人可以帮助我如何在主要功能内移动堆栈吗?

When recursion occurs the "src", "dest" and "spare" references are swapped, so if you pass those references to the PrintRods() function it will output the stacks in the wrong order. 发生递归时,将交换“ src”,“ dest”和“ spare”引用,因此,如果将这些引用传递给PrintRods()函数,它将以错误的顺序输出堆栈。

To get around this you could have an additional set of references to the stacks as parameters to the MoveDisks() function. 要解决此问题,您可以具有对堆栈的其他引用集,作为MoveDisks()函数的参数。

Try this (it is not pretty but it works): 试试这个(虽然不漂亮,但是可以用):

#include<iostream>
#include<stack>
using namespace std;
int setTowers(std::stack<int>& src)
{
    int disks;
    std::cout << "Enter number of disks: ";
    std::cin >> disks;

    for (int i = 0; i < disks; i++) src.push(disks - i);
    return disks;
}

void printRod(std::stack<int>tower)
{
    std::stack<int>temp;
    while (!tower.empty())
    {
        temp.push(tower.top());
        tower.pop();
    }
    if (!temp.size()) std::cout << "empty rod\n";
    else
    {
        while (!temp.empty())
        {
            std::cout << temp.top();
            temp.pop();
            if (temp.size() != 0) std::cout << ", ";
        }
        std::cout << '\n';
    }
}

void PrintRods(std::stack<int>& src, std::stack<int>& dest, std::stack<int>& spare)
{
    std::cout << "Source: "; printRod(src);
    std::cout << "Destination: ", printRod(dest);
    std::cout << "Spare: ", printRod(spare);
    std::cout << "----------------------------\n";
}

void MoveDisks(int disks, std::stack<int>& src, std::stack<int>& dest, std::stack<int>& spare,std::stack<int>&a,std::stack<int>&b,std::stack<int>&c,unsigned& stepsTaken)
{
    if (disks < 1)  return;
    else if (disks == 1)
    {
        stepsTaken++;
        dest.push(src.top());
        src.pop();
        std::cout << "Step #" << stepsTaken << ": Moved disk " << disks << '\n';
        PrintRods(a,b,c);
        return;
    }
    else
    {
        MoveDisks(disks - 1, src, spare, dest,a,b,c,stepsTaken);
        stepsTaken++;
        dest.push(src.top());
        src.pop();
        std::cout << "Step #" << stepsTaken << ": Moved disk " << disks << '\n';
        PrintRods(a,b,c);
        MoveDisks(disks - 1, spare, dest, src, a,b,c,stepsTaken);
    }
}
int main()
{
    std::stack<int> src;
    std::stack<int> dest;
    std::stack<int> spare;

    unsigned stepsTaken(0);
    int disks = setTowers(src);
    PrintRods(src, dest, spare);
    MoveDisks(disks, src, dest, spare,src,dest,spare,stepsTaken);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM