Quantcast

Jump to content


Photo

Towers of Hanoi with recursion


  • Please log in to reply
4 replies to this topic

#1 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 21 November 2009 - 04:31 PM

Right, so I'm just trying to make a function that lists out the steps for solving the Towers of Hanoi. Here's the code I got for the function:
void towers(int N, int presentPeg, int toPeg, int tempPeg)
{
	if(N > 0)
	{
		towers(N-1, presentPeg, toPeg, tempPeg);
		cout << "Move disc from Peg#" << presentPeg << " to Peg#" << toPeg << endl;
		towers(N-1, tempPeg, presentPeg, toPeg);
	}
}


The out for 3 discs is:
Move disc from Peg#1 to Peg#3
Move disc from Peg#1 to Peg#3
Move disc from Peg#2 to Peg#1
Move disc from Peg#1 to Peg#3
Move disc from Peg#2 to Peg#1
Move disc from Peg#2 to Peg#1
Move disc from Peg#3 to Peg#2


Which is obviously wrong. I'm calling the function like so: "towers(n, 1, 3, 2);" where n=3. Can anyone spot a problem?

#2 jcrdude

jcrdude
  • Oh shit there's a thing here

  • 7001 posts


Users Awards

Posted 21 November 2009 - 05:37 PM

Seems awful awkward to call the function from within itself.... let me see what I can think up...

Ok... first off, you need to determine your first move. With that in mind, it should be handy to come up with a function for just that purpose.

int determineFirstMove (n, toPeg, tempPeg)
{
 if n%2 == 0
 {
 	return tempPeg
 }
 else
 {
 	return toPeg
 }
}

Edited by jcrboy, 21 November 2009 - 05:39 PM.


#3 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 21 November 2009 - 06:04 PM

Seems awful awkward to call the function from within itself.... let me see what I can think up...

Ok... first off, you need to determine your first move. With that in mind, it should be handy to come up with a function for just that purpose.

int determineFirstMove (n, toPeg, tempPeg)
{
 if n%2 == 0
 {
 	return tempPeg
 }
 else
 {
 	return toPeg
 }
}


That's what I meant by recursion :p I think it's the easiest way of solving the problem.

#4 Hydrogen

Hydrogen
  • Neocodex Co-Founder

  • 22213 posts


Users Awards

Posted 21 November 2009 - 06:11 PM

That's what I meant by recursion :p I think it's the easiest way of solving the problem.

It's the only way to solve this problem :p. You can't do it in the general case without recursion :p. I haven't looked at the code since I'm a bit busy at the moment but my advice is to solve it for three pegs and then abstract that to the general case. The three pegs are your base case and everything will follow after that easily with recursion :p. Sorry, I know this post isn't very helpful :p.

#5 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 21 November 2009 - 06:39 PM

It's the only way to solve this problem :p. You can't do it in the general case without recursion :p. I haven't looked at the code since I'm a bit busy at the moment but my advice is to solve it for three pegs and then abstract that to the general case. The three pegs are your base case and everything will follow after that easily with recursion :p. Sorry, I know this post isn't very helpful :p.


Never mind, got it working. But that's pretty much what I was doing. I just had some a variables in the wrong place. =P

revised:
void towers(int N, int presentPeg, int tempPeg, int toPeg);//new header
...
towers(n, 1, 2, 3);

Edited by Melchoire, 21 November 2009 - 06:41 PM.



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users