Solve the Tower of Hanoi problem.
More...
#include <iostream>
Solve the Tower of Hanoi problem.
◆ main()
Main function
74 std::cout <<
"\nEnter number of discs : ";
77 for (
int i = no; i > 0; i--) {
78 F.values[F.top++] = i;
Definition: tower_of_hanoi.cpp:11
int top
top tower ID
Definition: tower_of_hanoi.cpp:15
void show(const struct tower *const F, const struct tower *const T, const struct tower *const U)
Definition: tower_of_hanoi.cpp:19
void TH(int n, tower *From, tower *Using, tower *To)
Definition: tower_of_hanoi.cpp:52
◆ mov()
Move one disc from one tower to another
- Parameters
-
| [in,out] | From | tower to move disk from |
| [in,out] | To | tower to move disk to |
int values[10]
Values in the tower.
Definition: tower_of_hanoi.cpp:13
◆ show()
| void show |
( |
const struct tower *const |
F, |
|
|
const struct tower *const |
T, |
|
|
const struct tower *const |
U |
|
) |
| |
Display the towers
22 for (
int i = 0; i < F->
top; i++) {
26 for (
int i = 0; i < U->
top; i++) {
30 for (
int i = 0; i < T->
top; i++) {
◆ TH()
Recursive algorithm to solve the puzzle
- Parameters
-
| [in] | n | starting number of disks |
| [in,out] | From | tower to move disks from |
| [in,out] | Using | temporary tower for the puzzle |
| [in,out] | To | tower to move disk to |
55 show(From, To, Using);
57 TH(n - 1, From, To, Using);
59 show(From, To, Using);
60 TH(n - 1, Using, From, To);
void mov(tower *From, tower *To)
Definition: tower_of_hanoi.cpp:39