Skip to content

Commit 70337ef

Browse files
adding towers of hanoi recursive cpp solution. updated readme to include necessary links
1 parent b35f859 commit 70337ef

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,8 +1290,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar mais pessoas a co
12901290
</a>
12911291
</td>
12921292
<td> <!-- C++ -->
1293-
<a href="./CONTRIBUTING.md">
1294-
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
1293+
<a href="./src/cpp/TorreDeHanoi.cpp">
1294+
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/cplusplus/cplusplus-original.svg" />
12951295
</a>
12961296
</td>
12971297
<td> <!-- Java -->

src/cpp/TorreDeHanoi.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Recursive Towers of Hanoi Solution
3+
Thomas Martin - 2023
4+
https://github.com/thomas-martin-uf
5+
6+
Problem Description: (from wikipedia)
7+
The Tower of Hanoi is a mathematical game or puzzle consisting of three rods and a number of disks of various diameters,
8+
which can slide onto any rod.
9+
10+
The puzzle begins with the disks stacked on one rod in order of decreasing size, the smallest at the top,
11+
thus approximating a conical shape.
12+
13+
The objective of the puzzle is to move the entire stack to the last rod, obeying the following rules:
14+
15+
Only one disk may be moved at a time.
16+
17+
Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty rod.
18+
19+
No disk may be placed on top of a disk that is smaller than it.
20+
21+
With 3 disks, the puzzle can be solved in 7 moves.
22+
23+
The minimal number of moves required to solve a Tower of Hanoi puzzle is 2^n − 1, where n is the number of disks.
24+
25+
26+
*/
27+
28+
#include <iostream>
29+
using std::cout;
30+
31+
size_t solve_tower(int n, char first, char second, char third)
32+
{
33+
34+
// recursive base case
35+
if (n == 1)
36+
{
37+
cout << "Move disk 1 from " << first << " to " << third << "\n";
38+
return 1;
39+
}
40+
41+
// move n-1 disks from first to other using second as a placeholder
42+
size_t num_moves = solve_tower(n - 1, first, second, third);
43+
44+
// move the nth disk from first to second
45+
cout << "Move disk " << n << " from " << first << " to " << second << "\n";
46+
47+
// move the n-1 disks from third to second using first as an placeholder
48+
num_moves += solve_tower(n - 1, third, first, second);
49+
50+
// return the total moves plus 1
51+
return num_moves + 1;
52+
}
53+
54+
int main()
55+
{
56+
57+
// names of rods
58+
char a = 'A';
59+
char b = 'B';
60+
char c = 'C';
61+
62+
// number of disks to move in puzzle
63+
size_t num_disk = 4;
64+
65+
// find the total number of moves needed to solve
66+
size_t num_moves = solve_tower(num_disk, a, b, c);
67+
68+
// output result
69+
cout << "Total moves needed to solve: " << num_moves;
70+
return 0;
71+
}

0 commit comments

Comments
 (0)