河内的塔,一举一动

时间:2013-04-08 01:53:04

标签: c++ towers-of-hanoi

我需要写一个河内程序的递归塔,我已经完成了,但它需要输出由星星代表的光盘的位置,这就是我遇到的麻烦。每次移动时,它都需要输出光盘位置的图片表示。我为3张光盘编写了代码,但是我需要它更灵活,适用于3,4或5.这是我到目前为止的代码。

// This program displays a solution to the Towers of
// Hanoi game.
#include <iostream>
using namespace std;

// Function prototype
void moveDiscs(int, int, int, int, int);

int main()
{
   int count = 0;
   int NUM_DISCS = 3;   // Number of discs to move
   const int FROM_PEG = 1;    // Initial "from" peg
   const int TO_PEG = 3;      // Initial "to" peg
   const int TEMP_PEG = 2;    // Initial "temp" peg

   // Play the game.
   moveDiscs(NUM_DISCS, FROM_PEG, TO_PEG, TEMP_PEG, count);
   cout << "All the pegs are moved!\n";

   system("PAUSE");
   return 0;
}

//***************************************************
// The moveDiscs function displays a disc move in   *
// the Towers of Hanoi game.                        *
// The parameters are:                              *
//    num:     The number of discs to move.         *
//    fromPeg: The peg to move from.                *
//    toPeg:   The peg to move to.                  *
//    tempPeg: The temporary peg.                   *
//***************************************************

void moveDiscs(int num, int fromPeg, int toPeg, int tempPeg, int count)
{
   if (num > 0)
   {  
      moveDiscs(num - 1, fromPeg, tempPeg, toPeg, count);
      cout << "Move a disc from peg " <<fromPeg
           << " to peg " << toPeg <<endl;
           count++; 
           if (fromPeg == 1 && toPeg == 3)
           {
                       if (num == 1)
                       {
                          if(count == 3)
                          cout<<"       *"<<endl<< "       *"<<endl<< "       *"<<endl;
                          else 
                          cout << "*"<< endl <<"*      *"<<endl;
                       }
                       else
                       cout << "   *"<<endl <<"   *   *"<<endl;
           }
           if (fromPeg==1 && toPeg==2)
           cout << "*  *  *"<<endl;
           if (fromPeg ==3 && toPeg==2)
           cout << "   *"<<endl <<"*  *"<<endl; 
           if (fromPeg==2 && toPeg==1)
           cout << "*  *  *"<<endl;
           if (fromPeg==2 && toPeg==3)
           cout << "       *"<< endl <<"*      *"<<endl;
      moveDiscs(num - 1, tempPeg, toPeg, fromPeg, count);
   }
}

1 个答案:

答案 0 :(得分:4)

虽然这不是您问题的直接答案 - 我想您可能会发现它有用。不久前我做了河内塔示例:

LIVE DEMO

#include <algorithm>
#include <iterator>
#include <iostream>
#include <ostream>
#include <vector>

using namespace std;

using HanoiPin = vector<unsigned>;

template<class PostAction>
void move_hanoi
(
    HanoiPin &from, HanoiPin &to, HanoiPin &temp,
    size_t count, PostAction post
)
{
    if(count>1)
    {
        move_hanoi(from, temp, to, count-1, post);
        move_hanoi(from, to, temp, 1, post);
        move_hanoi(temp, to, from, count-1, post);
    }
    else
    {
        to.push_back(from.back());
        from.pop_back();
        post();
    }
}

int main()
{
    auto count=5;
    HanoiPin a,b,c;
    generate_n(back_inserter(a), count, [=]() mutable
    {
        return count--;
    });
    auto print_hanoi=[](HanoiPin &h)
    {
        cout << "|";
        for(auto x : h)
            cout << x << "-";
        cout << endl;
    };
    auto printer=[&]
    {
        print_hanoi(a);
        print_hanoi(b);
        print_hanoi(c);
        cout << endl;
    };
    printer();
    move_hanoi(a,b,c,a.size(),printer);
}

输出是:

|5-4-3-2-1-
|
|

|5-4-3-2-
|1-
|

|5-4-3-
|1-
|2-

|5-4-3-
|
|2-1-

|5-4-
|3-
|2-1-

|5-4-1-
|3-
|2-

|5-4-1-
|3-2-
|

|5-4-
|3-2-1-
|

|5-
|3-2-1-
|4-

|5-
|3-2-
|4-1-

|5-2-
|3-
|4-1-

|5-2-1-
|3-
|4-

|5-2-1-
|
|4-3-

|5-2-
|1-
|4-3-

|5-
|1-
|4-3-2-

|5-
|
|4-3-2-1-

|
|5-
|4-3-2-1-

|1-
|5-
|4-3-2-

|1-
|5-2-
|4-3-

|
|5-2-1-
|4-3-

|3-
|5-2-1-
|4-

|3-
|5-2-
|4-1-

|3-2-
|5-
|4-1-

|3-2-1-
|5-
|4-

|3-2-1-
|5-4-
|

|3-2-
|5-4-1-
|

|3-
|5-4-1-
|2-

|3-
|5-4-
|2-1-

|
|5-4-3-
|2-1-

|1-
|5-4-3-
|2-

|1-
|5-4-3-2-
|

|
|5-4-3-2-1-
|