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
Source: favtutor.com
tower of hanoi
// C++ recursive function to // solve tower of hanoi puzzle #include <bits/stdc++.h> using namespace std; void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) { if (n == 0) { return; } towerOfHanoi(n - 1, from_rod, aux_rod, to_rod); cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << endl; towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); } // Driver code int main() { int N = 3; // A, B and C are names of rods towerOfHanoi(N, 'A', 'C', 'B'); return 0; } // This is code is contributed by rathbhupendra
Source: www.geeksforgeeks.org
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); }
tower of hanoi
# Recursive Python function to solve tower of hanoi def TowerOfHanoi(n , from_rod, to_rod, aux_rod): if n == 0: return TowerOfHanoi(n-1, from_rod, aux_rod, to_rod) print("Move disk",n,"from rod",from_rod,"to rod",to_rod) TowerOfHanoi(n-1, aux_rod, to_rod, from_rod) # Driver code n = 4 TowerOfHanoi(n, 'A', 'C', 'B') # A, C, B are the name of rods # Contributed By Harshit Agrawal
Source: www.geeksforgeeks.org
tower of hanoi
#include <iostream> #include <math.h> struct Node{ int diam; Node* next;}; class Stack { private: Node* top; public: Stack(); bool push(int); int pop(); int peek(); int count(); void print(); bool isEmpty(); }; Stack::Stack(){ top = NULL; } bool Stack::push(int diam){ bool status = false; Node* temp = new Node(); temp->diam = diam; temp->next = top; top = temp; if(top->diam == diam){ status = true; } return status; } bool Stack::isEmpty(){ bool status = false; if(top == NULL){ status = true; } return status; } int Stack::pop(){ int value = INT_MIN; if(top){ Node* temp = top; value = temp->diam; top = temp->next; delete(temp); } return value; } int Stack::count(){ int count = 0; Node* temp = top; while(temp){ count++; temp = temp->next; } return count; } int Stack::peek(){ int value = INT_MIN; if(top){ value = top->diam; } return value; } void Stack::print(){ Node* temp = top; while(temp){ std::cout << temp->diam << ' '; temp = temp->next; } } void moveDisk(Stack *source, Stack *destination){ int tower1Top = source->pop(); int tower2Top = destination->pop(); if(tower1Top == INT_MIN){ source->push(tower2Top); } else if(tower2Top ==INT_MIN){ destination->push(tower1Top); } else if(tower1Top > tower2Top){ source->push(tower1Top); source->push(tower2Top); } else{ destination->push(tower2Top); destination->push(tower1Top); } } int main() { Stack towerOne; Stack towerTwo; Stack towerThree; int towerSize = 10; if(towerOne.isEmpty() && towerTwo.isEmpty() && towerThree.isEmpty()){ std::cout << "Three empty stacks created" << std::endl; } std::cout << '\n'; std::cout << "Putting " << towerSize << " plates of subsequently smaller diameter on towerOne" << std::endl; for(int i=towerSize; i>0; i--){ towerOne.push(i); } std::cout << '\n'; std::cout << "Printing Tower One: " << std::endl; towerOne.print(); std::cout << '\n'; std::cout << '\n'; std::cout << "Testing pop() on Tower One (removing top value):" << std::endl; std::cout << towerOne.pop() << std::endl; std::cout << "Tower Size is now " << towerSize-1 << std::endl; std::cout << '\n'; std::cout << "Printing updated list:" << std::endl; towerOne.print(); std::cout << '\n'; std::cout << '\n'; std::cout << "Testing peek()" << std::endl; std::cout << towerOne.peek() << std::endl; std::cout << '\n'; std::cout << "Testing count()" << std::endl; std::cout << towerOne.count() << std::endl; std::cout << '\n'; int total_moves = pow(2, towerOne.count()) - 1; std::cout << "Running Towers of Hanoi algorithm" << std::endl; if(towerOne.count()%2 != 0){ for(int i=1; i<=total_moves; i++){ if(i%3==1){ moveDisk(&towerOne, &towerThree); } else if(i%3==2){ moveDisk(&towerOne, &towerTwo); } else if(i%3==0){ moveDisk(&towerTwo, &towerThree); } } } else if(towerOne.count()%2==0){ for(int i=1; i<=total_moves; i++){ if(i%3==1){ moveDisk(&towerOne, &towerTwo); } else if(i%3==2){ moveDisk(&towerOne, &towerThree); } else if(i%3==0){ moveDisk(&towerTwo, &towerThree); } } } std::cout << "Checking Tower Three count is equal to " << towerSize-1 << std::endl; std::cout << "Tower Three Count: " << towerThree.count() << std::endl; std::cout << "\n"; std::cout << "Checking Tower Three elements are in ascending order " << std::endl; towerThree.print(); std::cout << "\n"; }
Source: alecgarza96.medium.com