2

I have been trying to figure out how to implement a recursive function with just 3 parameters: Tower(Disk, from, to). I did a lot of research on google. All the results came up with the 4 parameters recursive function, (disk, a, b, c). Any help will be greatly appreciated.

EDIT:

typedef void (*towersActionFn)(unsigned short fromPost, unsigned short endPost);

void solveTowers(unsigned short nDisks, unsigned short start, unsigned short end, towersActionFn f);

the towersActionFn is only used to display the solution.

ylhtravis
  • 105
  • 1
  • 2
  • 10
  • If you've been trying to figure it out, how about posting the code you have so far instead of leaving 100% of the legwork to other people? – ildjarn Mar 30 '11 at 01:04

2 Answers2

1
//Pegs are counted 0 to 2

void Tower(int Disk, int Startpeg, int Endpeg)
{
    if(Disc <= 0)  //Sanity check
        return;

    if(Disc == 1)
        cout << "Move disk from "<<Startpeg<<" to "<<Endpeg<<endl;
    else
    {
        int Other = 3 - Startpeg - Endpeg;
        Tower(Disc-1, Startpeg,  Other);
        Tower(1, Startpeg, Endpeg); //Inline output would do here... Calling for message consistency.
        Tower(Disc-1, Other, Endpeg);
    }
}
Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
0

Here is the Algorithm

Community
  • 1
  • 1
Sadique
  • 22,572
  • 7
  • 65
  • 91
  • 1
    Thank you for the respond, but i believe i didnt word my question correctly. I am trying to implement the function with just the starting peg, and the destination peg in the parameter, and of course the # of disks. For example. void Tower(int Disk, int Startpeg, int Endpeg) – ylhtravis Mar 30 '11 at 01:10
  • So, how have you defined that function, where are you stuck? – Sadique Mar 30 '11 at 01:21