9/30/2017 Towers of Hanoi ~ Programming Tutorials by SourceTricks
Programming Tutorials by SourceTricks
(http://www.sourcetricks.com/)
3:19:00 PM SourceTricks (https://www.blogger.com/profile/17421383822093860960)
Programming interview questions and answers (http://www.sourcetricks.com/search/label/Programming%20interview%20questions%20and%20answers)
2 comments (http://www.sourcetricks.com/2014/01/towers-of-hanoi.html#comment-form)
Towers of Hanoi (http://www.sourcetricks.com/2014/01/towers-of-hanoi.html)
Approach to solve the Towers of Hanoi game
1. We need to move the n rings from the first pole to target third pole using the second pole as buffer.
2. First move the n-1 rings from first to a second pole using the third.
3. Then move the remaining big ring from first to the third.
4. Now move all the n-1 rings from the second pole to the target third pole on top of the nth (which is big).
5. For moving the n-1 rings from one pole to another pole, use the remaining pole as a buffer using recursion.
C++ program to to solve the Towers of Hanoi game
http://www.sourcetricks.com/2014/01/towers-of-hanoi.html#.Wc-o-miCzIU 1/4
9/30/2017 Towers of Hanoi ~ Programming Tutorials by SourceTricks
#include <iostream>
using namespace std;
#define MAX 4
int fromPoleMain[MAX] = {4,3,2,1};
int tmpPoleMain[MAX];
int toPoleMain[MAX];
void printPole(int *pole)
{
if (fromPoleMain == pole)
{
cout << "ONE ";
return;
}
if (tmpPoleMain == pole)
{
cout << "TWO ";
return;
}
if (toPoleMain == pole)
{
cout << "THREE ";
return;
}
void move(int* fromPole, int* toPole)
{
int i,j;
for(i = 0; i<MAX;i++)
{
if(fromPole[i] == 0)
break;
}
for(j = 0; j<MAX;j++)
{
if(toPole[j] == 0)
break;
}
toPole[j] = fromPole[i-1];
printPole(fromPole);
cout << ": posi " << i << " to --> ";
printPole(toPole);
cout << ": at posi " << j ;
cout << " moving " << toPole[j] <<endl;
fromPole[i-1] = 0;
void TowersOfHanoi(int n, int* fromPole, int* toPole, int* tmpPole )
{
if (n==0) return; // no more rings to move.
TowersOfHanoi(n-1, fromPole, tmpPole, toPole);
move(fromPole,toPole);
TowersOfHanoi(n-1, tmpPole, toPole, fromPole);
}
int main()
{
printPole(fromPoleMain); printPole(toPoleMain); printPole(tmpPoleMain); cout << ": " << endl;
TowersOfHanoi(MAX,fromPoleMain,toPoleMain,tmpPoleMain);
cout << "Finally target POLE: ";
for(int i = 0; i<MAX;i++)
{
cout << toPoleMain[i] << " ";
}
cout << endl;
return 0;
}
Output:-
http://www.sourcetricks.com/2014/01/towers-of-hanoi.html#.Wc-o-miCzIU 2/4
9/30/2017 Towers of Hanoi ~ Programming Tutorials by SourceTricks
ONE THREE TWO :
ONE : posi 4 to --> TWO : at posi 0 moving 1
ONE : posi 3 to --> THREE : at posi 0 moving 2
TWO : posi 1 to --> THREE : at posi 1 moving 1
ONE : posi 2 to --> TWO : at posi 0 moving 3
THREE : posi 2 to --> ONE : at posi 1 moving 1
THREE : posi 1 to --> TWO : at posi 1 moving 2
ONE : posi 2 to --> TWO : at posi 2 moving 1
ONE : posi 1 to --> THREE : at posi 0 moving 4
TWO : posi 3 to --> THREE : at posi 1 moving 1
TWO : posi 2 to --> ONE : at posi 0 moving 2
THREE : posi 2 to --> ONE : at posi 1 moving 1
TWO : posi 1 to --> THREE : at posi 1 moving 3
ONE : posi 2 to --> TWO : at posi 0 moving 1
ONE : posi 1 to --> THREE : at posi 2 moving 2
TWO : posi 1 to --> THREE : at posi 3 moving 1
Finally target POLE: 4 3 2 1
Related Posts:
Linked list nth last element (http://www.sourcetricks.com/2012/07/linked-list-nth-last-element.html)
Write a program to find in a linked list nth last element The approach:- Maintain 2 pointers to head of the list. Move 1st pointer n - 1 elements. Now move… Read More
(http://www.sourcetricks.com/2012/07/linked-list-nth-last-element.html)
Compute the endianness (http://www.sourcetricks.com/2012/07/compute-endianness.html)
Write a program to compute the endianness of a system Endianess In computing, the term endian or endianness refers to the ordering of individually addressable…
Read More (http://www.sourcetricks.com/2012/07/compute-endianness.html)
Shuffle a pack of cards (http://www.sourcetricks.com/2012/07/shuffle-pack-of-cards.html)
Write a program to shuffle a pack of cards The approach:- Initialize a cards array and set the seed for rand based on time. For each array index generate a … Read
More (http://www.sourcetricks.com/2012/07/shuffle-pack-of-cards.html)
Find if two strings are anagrams (http://www.sourcetricks.com/2012/07/find-if-two-strings-are-anagrams.html)
Write a program to find if two strings are anagrams Anagram - Wikipedia An anagram is a type of word play, the result of rearranging the letters of a word or … Read
More (http://www.sourcetricks.com/2012/07/find-if-two-strings-are-anagrams.html)
Reverse a linked list (http://www.sourcetricks.com/2012/07/reverse-linked-list.html)
Write a program to construct and reverse a linked list We write an iterative approach to reverse a linked list. The approach is to start from the head node an… Read
More (http://www.sourcetricks.com/2012/07/reverse-linked-list.html)
← Newer Post (http://www.sourcetricks.com/2014/01/sax-parser-to-read-xml-file-in-java.html)
Older Post → (http://www.sourcetricks.com/2014/01/find-loop-in-linked-list-and-identify.html)
2 comments :
chenmeinv0 (https://www.blogger.com/profile/04850790224822937115) December 29, 2016 at 11:16 AM (http://www.sourcetricks.com/2014/01/towers-of-
hanoi.html?showComment=1482990396577#c5837402676282437270)
gucci belts (http://www.gucci-outlet.org)
air jordan shoes (http://www.jordanshoes.us.org)
oakley sunglasses outlet (http://www.oakley-sunglasses.net.co)
oakley sunglasses outlet (http://www.cheapoakleysunglasses.net.co)
ralph lauren outlet (http://www.poloralphlauren.nom.co)
air jordan shoes (http://www.cheapjordansshoes.us.com)
ralph lauren (http://www.ralphlaurenoutlet.nom.co)
coach outlet store online (http://www.coachfactoryoutlet-online.us.org)
ray ban sunglasses outlet (http://www.ray-banssunglasses.com.co)
tory burch sale (http://www.cheaptoryburchoutlet.com)
2016.12.29xukaimin
Reply
Eon Mech (https://www.blogger.com/profile/10147433905545861008) January 8, 2017 at 11:27 AM (http://www.sourcetricks.com/2014/01/towers-of-hanoi.html?
showComment=1483855064506#c6125981469315446088)
thanks for sharing , very informative stuff
red hat linux training in chennai (http://sysacad.in/portfolio-view/linux-training-in-chennai/) | rhce courses in chennai (http://sysacad.in/portfolio-view/linux-training-in-
chennai/) | red hat training in chennai (http://sysacad.in/portfolio-view/linux-training-in-chennai/) |red hat courses in chennai (http://sysacad.in/portfolio-view/vmware-
training-in-chennai/)
Reply
http://www.sourcetricks.com/2014/01/towers-of-hanoi.html#.Wc-o-miCzIU 3/4
9/30/2017 Towers of Hanoi ~ Programming Tutorials by SourceTricks
Enter your comment...
Comment as: Unknown (Goo Sign out
Publish Preview Notify me
(https://www.blogger.com/comment-iframe.g?blogID=7748177500667831327&postID=861168250104027697&blogspotRpcToken=3526398)
Subscribe to: Post Comments ( Atom ) (http://www.sourcetricks.com/feeds/861168250104027697/comments/default)
Tutorial Pages
Java Tutorials (http://www.sourcetricks.com/p/java-tutorials.html)
Micro Services (http://www.sourcetricks.com/p/microservices.html)
Java Interview Questions (http://www.sourcetricks.com/2014/06/core-java-interview-questions.html)
Scala Tutorials (http://www.sourcetricks.com/p/scala-tutorials.html)
Programming in C++ (http://www.sourcetricks.com/p/programming-in-c.html)
Programming interview questions and answers in C++ (http://www.sourcetricks.com/p/programming-interview-questions-and.html)
Data Structures using C++ (http://www.sourcetricks.com/p/data-structures-using-c.html)
Algorithms in C++ (http://www.sourcetricks.com/p/algorithms-in-c.html)
Design Patterns using C++ (http://www.sourcetricks.com/p/design-patterns-using-c.html)
Android (http://www.sourcetricks.com/p/android.html)
HTML, CSS and Javascript (http://www.sourcetricks.com/p/html-css-and-javascript.html)
UML Notations (http://www.sourcetricks.com/2008/05/uml-generalization.html)
Tag Cloud
Java (http://www.sourcetricks.com/search/label/Java) CPP
(http://www.sourcetricks.com/search/label/CPP) Programming interview questions and
answers
(http://www.sourcetricks.com/search/label/Programming%20interview%20questions%20and%
20answers) Design Patterns (http://www.sourcetricks.com/search/label/Design%20Patterns) Scala
(http://www.sourcetricks.com/search/label/Scala) Android (http://www.sourcetricks.com/search/label/Android) Algorithms
(http://www.sourcetricks.com/search/label/Algorithms) Data Structures (http://www.sourcetricks.com/search/label/Data%20Structures) Micro Services
(http://www.sourcetricks.com/search/label/Micro%20Services) JavaScript (http://www.sourcetricks.com/search/label/JavaScript) tools
(http://www.sourcetricks.com/search/label/tools) UML (http://www.sourcetricks.com/search/label/UML) html (http://www.sourcetricks.com/search/label/html)
http://www.sourcetricks.com/2014/01/towers-of-hanoi.html#.Wc-o-miCzIU 4/4