Answers for "Tower of Hanoi"

1

tower of hanoi

//code by Soumyadeep Ghosh
//insta : @soumyadepp
//linked in: https://www.linkedin.com/in/soumyadeep-ghosh-90a1951b6/
#include <bits/stdc++.h>

using namespace std;

void toh(int n,char a,char b, char c)
{
  if(n>0)
    {
        /*move n-1 disks from a to b using c*/
        toh(n-1,a,c,b);
        /*move a disc from a to c using b and display this step performed. Also note that a and c are different in the next called function*/
        cout<<"Move a disk from "<<a<<" to "<<c<<endl;
        toh(n-1,b,a,c);
    }
}
int main()
{
  int n;
  cin>>n;
  //names of the disks are a,b,c
  toh(n,'a','b','c');
  return 0;
}
//thank you!
Posted by: Guest on November-05-2020
0

tower of hanoi

/// find total number of steps 
int towerOfHanoi(int n) {
  /// pow(2,n)-1
  if (n == 0) return 0;
  
  return towerOfHanoi(n - 1) + 1 + towerOfHanoi(n - 1);
}
Posted by: Guest on April-04-2021
0

tower of hanoi

#include <iostream>

using namespace std;
void solve(int n,int s,int d,int h)
{
    if(n==1)
    {
        cout<<"MOVED DISK "<<n<<" from"<<s<<"-->"<<d<<endl;
        return;
    }
    solve(n-1,s,h,d);
    cout<<"MOVED DISK "<<n<<"from"<<s<<"-->"<<d<<endl;
    solve(n-1,h,d,s);
}
int main()
{
    int n;
    cout<<"ENTER THE NUMBER: ";
    cin>>n;
    int s=1;
    int h=2;
    int d=3;
    solve(n,s,d,h);
    return 0;
}
Posted by: Guest on September-23-2021
0

Tower of Hanoi

def towerOfHanoi(N , source, destination, auxiliary):
	if N==1:
		print("Move disk 1 from source",source,"to destination",destination)
		return
	towerOfHanoi(N-1, source, auxiliary, destination)
	print("Move disk",N,"from source",source,"to destination",destination)
	towerOfHanoi(N-1, auxiliary, destination, source)
		
# Driver code
N = 3
towerOfHanoi(N,'A','B','C')
# A, C, B are the name of rods
Posted by: Guest on August-12-2021

Code answers related to "Tower of Hanoi"

Python Answers by Framework

Browse Popular Code Answers by Language