Towers of Hanoi Problem in C++: Solving the Classic Puzzle with Recursion

  • Comp Sci
  • Thread starter NickPatey
  • Start date
  • Tags
    C++
In summary: I do not have access to the problem mentioned in this conversation, so I cannot summarize the content.
  • #1
NickPatey
2
0
I have to write a C++ program for the problem listed below, but i can't get it working properly, can anyone find the problem:

Question:
5.36 (Towers of Hanoi) Every budding computer scientist must grapple with certain classic problems,
and the Towers of Hanoi (see Fig. 5.19) is one of the most famous of these. Legend has it that
in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another.
The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by decreasing
size. The priests are attempting to move the stack from this peg to a second peg under the constraints
that exactly one disk is moved at a time, and at no time may a larger disk be placed above a smaller
disk. A third peg is available for temporarily holding the disks. Supposedly the world will end when
the priests complete their task, so there is little incentive for us to facilitate their efforts.

Let’s assume that the priests are attempting to move the disks from peg 1 to peg 3. We wish to
develop an algorithm that will print the precise sequence of disk-to-disk peg transfers.
If we were to approach this problem with conventional methods, we’d rapidly find ourselves
hopelessly knotted up in managing the disks. Instead, if we attack the problem with recursion in
mind, it immediately becomes tractable. Moving n disks can be viewed in terms of moving only
n – 1 disks (and hence the recursion) as follows:
a) Move n – 1 disks from peg 1 to peg 2, using peg 3 as a temporary holding area.
b) Move the last disk (the largest) from peg 1 to peg 3.
c) Move the n – 1 disks from peg 2 to peg 3, using peg 1 as a temporary holding area.
The process ends when the last task involves moving n = 1 disk, i.e., the base case. This is
accomplished by trivially moving the disk without the need for a temporary holding area.
Write a program to solve the Towers of Hanoi problem. Use a recursive function with four
parameters:
a) The number of disks to be moved
b) The peg on which these disks are initially threaded
c) The peg to which this stack of disks is to be moved
d) The peg to be used as a temporary holding area
Your program should print the precise instructions it will take to move the disks from the
starting peg to the destination peg. For example, to move a stack of three disks from peg 1 to peg 3,
your program should print the following series of moves:
1 →3 (This means move one disk from peg 1 to peg 3.)
1 → 2
3 → 2
1 →3
2 →1
2 →3
1 →3

What i have so far:
Code:
// towers.cpp : Defines the entry point for the console application.//

#include "stdafx.h"


int main(void)
{
	int n = 0;
	int sp = 0;
	int ep = 0;
	int hp = 0;
	int move (int n, int sp, int ep, int hp);
	int wait;
	printf("Enter number of disks");
	scanf("%d", &n);
	printf("Enter start peg (1, 2, or 3): ");
	scanf("%d", &sp);
	printf("Enter end peg number (1, 2, or 3): ");
	scanf("%d", &ep);

	if ( sp == ep ){
		printf("Nice try");
		return 0;
	}
	if (( sp == 1 && ep ==2) || (sp ==2 && ep == 1)) hp = 3;
	if (( sp == 1 && ep ==3) || (sp ==3 && ep == 1)) hp = 2;
	if (( sp == 2 && ep ==3) || (sp ==3 && ep == 2)) hp = 1;

	printf("Now to blow your mind as we move %d disks from peg %d to peg %d using %d as a holding peg never allowing a larger disk to be on top\n\n", n, sp , ep , hp);

	move (n , sp , ep , hp);
	scanf_s("%d", &wait);
	return 0;
}
int move ( int n, int sp, int ep, int hp )
{
	if (n==1)
	{
		printf("Move disk from %d--->%d", sp , ep);
	}
	else
	{
		move ( (n-1), sp, hp, ep );
		printf("Move disk from %d--->%d", sp , ep);
		move ( (n-1), hp, ep, sp );
	}
}
 
Physics news on Phys.org
  • #2
Nvm figured it out haha:

Code:
// towers.cpp : Defines the entry point for the console application.//

#include <stdafx.h>

int move (int n, int sp, int ep, int hp);

int main(void)
{
	int move (int n, int sp, int ep, int hp);
	int n = 0;
	int sp = 0;
	int ep = 0;
	int hp = 0;
	int wait;

	printf("Enter number of disks: ");
	scanf_s("%d", &n);
	printf("Enter start peg (1-3): ");
	scanf_s("%d", &sp);
	printf("Enter end peg (1-3):   ");
	scanf_s("%d", &ep);

	if ( sp == ep ){
		printf("Nice try");
		return 0;
	}

	if (( sp == 1 && ep ==2) || (sp ==2 && ep == 1)) hp = 3;
	if (( sp == 1 && ep ==3) || (sp ==3 && ep == 1)) hp = 2;
	if (( sp == 2 && ep ==3) || (sp ==3 && ep == 2)) hp = 1;

	printf("\n\nMove %d disks from peg %d to peg %d using %d as a holding peg\n\n", n, sp , ep , hp);

	move (n , sp , ep , hp);
	scanf_s("%d", &wait);

	return 0;
}
int move ( int n, int sp, int ep, int hp )
{

	if (n==1)
	{
		printf("Move disk from %d--->%d\n", sp , ep);
	}
	else
	{
		move ( (n-1), sp, hp, ep );
		printf("Move disk from %d--->%d\n", sp , ep);
		move ( (n-1), hp, ep, sp );
	}

	return n;

}
 
  • #3
Note, this is C.
 

1. What is the C++ Towers of Hanoi Problem?

The Towers of Hanoi problem is a mathematical puzzle that involves moving a stack of disks from one peg to another, while adhering to certain rules. It is commonly used as an exercise in programming, particularly in C++.

2. What are the rules of the Towers of Hanoi problem?

The rules of the Towers of Hanoi problem are:

  • Only one disk can be moved at a time.
  • Each move consists of taking the top disk from one stack and placing it on another stack.
  • A disk can only be placed on top of a larger disk or an empty peg.

3. How many moves are required to solve the Towers of Hanoi problem?

The minimum number of moves required to solve the Towers of Hanoi problem with n disks is 2n - 1. So, for example, if there are 3 disks, the minimum number of moves required is 23 - 1 = 7.

4. What is the algorithm for solving the Towers of Hanoi problem?

The algorithm for solving the Towers of Hanoi problem is:

  1. Move n-1 disks from the starting peg to the spare peg.
  2. Move the largest disk from the starting peg to the destination peg.
  3. Move the n-1 disks from the spare peg to the destination peg.

5. Can the Towers of Hanoi problem be solved recursively?

Yes, the Towers of Hanoi problem can be solved recursively. In fact, the recursive solution is the most commonly used and efficient method. The recursive algorithm follows the same steps as the non-recursive algorithm, but instead of moving n-1 disks at once, it moves one disk at a time. This process continues until all disks are moved to the destination peg.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
882
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
25
Views
5K
Back
Top