edited by
2,459 views
3 votes
3 votes

In Tower of Hanoi problem, when we move 3 disk , it will rotate like

Input : 3
Output : Disk 1 moved from A to C
         Disk 2 moved from A to B
         Disk 1 moved from C to B
         Disk 3 moved from A to C
         Disk 1 moved from B to A
         Disk 2 moved from B to C
         Disk 1 moved from A to C

Now if we move it in a recursive function , it looks like

void move(int n, char A, char B, char C) {
    if (n > 0) {
        move (n-1, A, C, B);
        printf("Move disk %d from pole %c to pole %c\n", n, A, C);
        move (n-1, B, A, C);
    } 
}

But there is no move from C to B in recursive function call, which should be 3rd call according to logic.

means there should be a call from move(n-1,C,B,A) which is not there.

Where am I missing plz help

---------------------------------------------------------------------------------------------------------------------------------

According to the link https://www.cs.cmu.edu/~cburch/survey/recurse/hanoiex.html

Some disk move I have shown here with recursion tree. Plz help me some moves of disk after this move

edited by

1 Answer

Best answer
1 votes
1 votes

function is like that

move(from_vertex,to_vertex,auxiliary_vertex)

void move(int n, char A, char B, char C)

The above $2$ taken from your question proves that you want to transfer $A$ to $B$ using $C$

How tower of hanoi works?

  • Move $n-1$ disks from $A \rightarrow C$ using $B$
  • Move $n^{th}$ disk of $A$ to $B$
  • Move all $n-1$ disks $C \rightarrow B$ using $A$

Code-:

#include<stdio.h>
void toh(int n,char from_peg,char to_peg,char via_peg)
{
if(n==1)
{
printf("MOVE DISK %d  FROM %c TO %c\n ",n,from_peg,to_peg);
}
else
{
toh(n-1,from_peg,via_peg,to_peg);
printf("MOVE DISK %d  FROM %c TO %c \n",n,from_peg,to_peg);
toh(n-1,via_peg,to_peg,from_peg);
}
}

int main()
{
int num;
printf("\n*****Tower of hanoi using 3 pegs*******\n");
printf("from peg A to peg B via peg C\n");
printf("enter the number of disks\n");
scanf("%d",&num);
toh(num,'A','B','C');
return 0;
}

Output-:

*****Tower of hanoi using 3 pegs*******
from peg A to peg B via peg C
enter the number of disks
3
MOVE DISK 1  FROM A TO B
MOVE DISK 2  FROM A TO C
MOVE DISK 1  FROM B TO C
MOVE DISK 3  FROM A TO B
MOVE DISK 1  FROM C TO A
MOVE DISK 2  FROM C TO B
MOVE DISK 1  FROM A TO B

Your issue -:

First of all your output is for the input transfer peg $A \text{to peg } C \text{via peg}B$

your issue in the code is

line 3rd

move (n-1, C, B, A);
selected by

Related questions

0 votes
0 votes
0 answers
1
iarnav asked May 27, 2017
635 views
how to Write a program in C which implements tower of Hanoi using recursion? and please explain lines of code if possible.It 'll be a great help.
0 votes
0 votes
0 answers
2
Ankit Garg 2 asked Nov 26, 2018
343 views
0 votes
0 votes
0 answers
3
rahul sharma 5 asked Nov 26, 2017
556 views
Can we solve Towers of honoi with DP? If yes,then what will be time and space complexity?
11 votes
11 votes
1 answer
4
dd asked Oct 31, 2016
2,828 views
In the tower of Hanoi $x , y , z$ are the positions and we are to move 10 disks from $x$ to $y$. What are the $128$th and $768$th moves ?(A) $x\rightarrow y$ and $x\right...