Solve the Tower of Hanoi problem.
More...
#include <iostream>
Solve the Tower of Hanoi problem.
◆ main()
Main function
65 {
67
69 U.top = 0;
70 T.top = 0;
71
72 int no;
73
74 std::cout <<
"\nEnter number of discs : ";
76
77 for (int i = no; i > 0; i--) {
78 F.values[F.top++] = i;
79 }
80
81 show(&F, &T, &U);
83
84 return 0;
85}
Definition tower_of_hanoi.cpp:11
int top
top tower ID
Definition tower_of_hanoi.cpp:15
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 |
39 {
43}
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
20 {
22 for (
int i = 0; i < F->
top; i++) {
24 }
26 for (
int i = 0; i < U->
top; i++) {
28 }
30 for (
int i = 0; i < T->
top; i++) {
32 }
33}
◆ 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 |
52 {
53 if (n == 1) {
55 show(From, To, Using);
56 } else {
57 TH(n - 1, From, To, Using);
59 show(From, To, Using);
60 TH(n - 1, Using, From, To);
61 }
62}
void mov(tower *From, tower *To)
Definition tower_of_hanoi.cpp:39