Slashfear's Programming challenge!!!
This time I am fully loaded with a programming Challenge, all the programming language's are allowed 😉.
Here is the Problem:
This is a classic problems. The Tower of Hanoi or Towers of Hanoi (also known as The Towers of Brahma) is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks neatly stacked in order of size on one rod, the smallest at the top, thus making a conical shape as shown below:
The objective of the puzzle is to move the entire stack to another rod, obeying the following rules:
-> Only one disk may be moved at a time.
-> Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod.
-> No disk may be placed on top of a smaller disk.
Let us assume that the priests are attempting to move the disks from peg 1 to peg 3. We wish to develop an algorithm that will print the precise sequence of peg-to-peg disk transfers.If we were to approach this problem with conventional methods, we would rapidly find ourselves hopelessly knotted up in
managing the disks.
Clue to solution: if we attack the problem with recursion in mind, it immediately becomes tractable. Moving n disks can be viewed in terms of moving only n - 1 disks (hence, the recursion), as follows:
a) Move n - 1 disks from peg 1 to peg 2, using peg 3 as a temporary holding area.
b) Move the last disk (the largest) from peg 1 to peg 3.
c) Move the n - 1 disks from peg 2 to peg 3, using peg 1 as a temporary holding area.
Sample output:
[B]Enter the starting number of disks: 4 1 --> 2 1 --> 3 2 --> 3 1 --> 2 3 --> 1 3 --> 2 1 --> 2 1 --> 3 2 --> 3 2 --> 1 3 --> 1 2 --> 3 1 --> 2 1 --> 3 2 --> 3 [/B]Lets see who finish this puzzle first....... let me give my solution later!!! ;-)
Don't try to google it, Its not going to help you out in improving your programming skill (this puzzle is only for those who wanna be a serious programmer 😁)
-Arvind(slashfear)