Tower Of Hanoi | Practice | GeeksforGeeks

The tower of Hanoi is a famous puzzle where we have three rods and N disks. The objective of the puzzle is to move the entire stack to another rod. You are given the number of discs N. Initially, these discs are in the rod 1. You need to print all the steps of discs movement so that all the discs reach the 3rd rod. Also, you need to find the total moves.
Note: The discs are arranged such that the top disc is numbered 1 and the bottom-most disc is numbered N. Also, all the discs have different sizes and a bigger disc cannot be put on the top of a smaller disc. Refer the provided link to get a better clarity about the puzzle.
Example 1:
Input:
N = 2
Output:
move disk 1 from rod 1 to rod 2
move disk 2 from rod 1 to rod 3
move disk 1 from rod 2 to rod 3
3
Explanation: For N=2 , steps will be
as follows in the example and total
3 steps will be taken.
Example 2:
Input:
N = 3
Output:
move disk 1 from rod 1 to rod 3
move disk 2 from rod 1 to rod 2
move disk 1 from rod 3 to rod 2
move disk 3 from rod 1 to rod 3
move disk 1 from rod 2 to rod 1
move disk 2 from rod 2 to rod 3
move disk 1 from rod 1 to rod 3
7
Explanation: For N=3 , steps will be
as follows in the example and total
7 steps will be taken.

Your Task:
You don't need to read input or print anything. You only need to complete the function toh() that takes following parameters: N (number of discs), from (The rod number from which we move the disc), to (The rod number to which we move the disc), aux (The rod that is used as an auxiliary rod) and prints the required moves inside function body (See the example for the format of the output) as well as return the count of total moves made. The total number of moves are printed by the driver code.
Please take care of the case of the letters.
Expected Time Complexity: O(2N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 16
0
sv10999127 hours ago
JAVA SOLUTION 1.1SEC 
 class Hanoi {      public long towOfhan(int n, int from ,int to, int aux)   {       if(n == 0){           return 1;       }        long i = towOfhan(n-1,from,aux,to);       System.out.println("move disk "+n + " from rod "+ from  +" to rod " + to );       long j = towOfhan(n-1,aux,to,from);               return i + j;   }
   public long toh(int N, int from, int to, int aux){         long steps = towOfhan(N,from,to,aux);       return steps-1;    }}
0
imabhishek026 days ago

    int count=0;
    long long toh(int n, int src, int dest, int help) {
       
       if(n>0)
       {
       toh(n-1,src,help,dest);
        
       cout<<"move disk " <<n<< " from rod "<<src<<" to rod "<<dest<<endl;
       count++;
       
       toh(n-1,help,dest,src);  
       }return count;
       
          }



+2
pickyourpillnow1 week ago
int count=0;   long long toh(int N, int from, int to, int aux) {       // Your code here      if(N>0){           toh(N-1,from,aux,to);           cout<<"move disk "<< N <<" from rod "<<from<<" to rod "<<to<<endl;           count++;           toh(N-1,aux,to,from);          }       return count;   }
0
deepakaryak91 week ago
//Python
 
class Solution:   def countMoves(self,n):       if n == 0:           return 0;       return 2 * self.countMoves(n - 1) + 1   def toh(self, N, fromm, to, aux):       # Your code here       if N == 1:           print("move disk " + str(1) + " from rod " + str(fromm) + " to rod " + str(to))           return 1                 else:           self.toh(N-1,fromm,aux,to)           print("move disk " + str(N) + " from rod " + str(fromm) + " to rod " + str(to))           self.toh(N-1,aux,to,fromm)           return self.countMoves(N)
+1
rohankundu8591 week ago
//C++ Solution
long long toh(int N, int from, int to, int aux) 
    {
        // Your code here
    int n=N;
    if(N==0)
    {
        return 0;
    }
      toh(N-1,from,aux,to);
      cout<<"move disk "<<N<<" from rod "<<from<<" to rod "<<to<<endl;
      toh(N-1,aux,to,from);
      return (pow(2,n)-1);
    }
0
rahuldebnath292 weeks ago
 long long count = 0;   long long toh(int N, int from, int to, int aux) {       // Your code here   if(N==1)   {       cout<<"move disk 1 from rod "<<from<<" to rod "<<to<<endl;       count++;       return count;   }   toh(N-1,from,aux,to);   cout<<"move disk "<<N<<" from rod "<<from<<" to rod "<<to<<endl;   count ++;   toh(N-1,aux,to,from);   return count;   }
0
rajsandhu19892 weeks ago
long long steps=0;
    long long toh(int N, int from, int to, int aux) {
        // Your code here
        if(N==1){
            cout<<"move disk 1 from rod "<<from<<" to rod "<<to<<endl;
            steps++;
            return steps;
        }
        toh(N-1,from,aux,to);
        cout<<"move disk "<<N<<" from rod "<<from<<" to rod "<<to<<endl;
        steps++;
        toh(N-1,aux,to,from);
        return steps;
    }
0
sambhavimukherjee102 weeks ago
    public:   // You need to complete this function
   // avoid space at the starting of the string in "move disk....."   long long count = 0;   long long toh(int N, int from, int to, int aux) {       if(N > 0){           toh(N-1, from, aux, to);           cout << "move disk " <<N<< " from rod " <<from<< " to rod " << to <<endl;           count++;           toh(N-1, aux, to, from);       }   return count;   }};
0
iscream1 month ago
 public long f(int n,Stack<Integer> fromRod,Stack<Integer> toRod,Stack<Integer> auxRod,int from,int to,int aux){       if(n ==1){           int disk = fromRod.pop();           System.out.println("move disk " + disk +" from rod " + from+" to rod " + to);           toRod.push(disk);           return 1;       }       return f(n-1,fromRod,auxRod,toRod,from,aux,to) + f(1,fromRod,toRod,auxRod,from,to,aux) + f(n-1,auxRod,toRod,fromRod,aux,to,from);   }      public long toh(int N, int from, int to, int aux) {       Stack<Integer> fromRod = new Stack<>();       for(int i=N;i>=1;i--){           fromRod.push(i);       }       Stack<Integer> toRod = new Stack<>();       Stack<Integer> auxRod = new Stack<>();       return f(N,fromRod,toRod,auxRod,from,to,aux);                 }
+1
dipanshusharma93131 month ago
// java solution
class Hanoi {    public long toh(int N, int from, int to, int aux) {       // Your code here       long count = 0;       if(N == 0){           return 0;       }              count += toh(N-1, from, aux, to);       System.out.println("move disk "+N+" from rod "+from+" to rod "+to);       count += toh(N-1, aux, to, from);              return ++count;   }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.