Skip to content

Commit 7f0fdeb

Browse files
fixing the move print out order as it was incorrect
1 parent 83b22d3 commit 7f0fdeb

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/cpp/TowerOfHanoi.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@
1212
1313
The objective of the puzzle is to move the entire stack to the last rod, obeying the following rules:
1414
15-
Only one disk may be moved at a time.
15+
Only one disk may be moved at a time.
1616
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.
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.
1818
19-
No disk may be placed on top of a disk that is smaller than it.
19+
No disk may be placed on top of a disk that is smaller than it.
2020
2121
With 3 disks, the puzzle can be solved in 7 moves.
2222
2323
The minimal number of moves required to solve a Tower of Hanoi puzzle is 2^n − 1, where n is the number of disks.
2424
25-
26-
*/
25+
*/
2726

2827
#include <iostream>
2928
using std::cout;
@@ -39,13 +38,13 @@ size_t solve_tower(int n, char first, char second, char third)
3938
}
4039

4140
// 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);
41+
size_t num_moves = solve_tower(n - 1, first, third, second);
4342

4443
// move the nth disk from first to second
45-
cout << "Move disk " << n << " from " << first << " to " << second << "\n";
44+
cout << "Move disk " << n << " from " << first << " to " << third << "\n";
4645

4746
// move the n-1 disks from third to second using first as an placeholder
48-
num_moves += solve_tower(n - 1, third, first, second);
47+
num_moves += solve_tower(n - 1, second, first, third);
4948

5049
// return the total moves plus 1
5150
return num_moves + 1;
@@ -60,7 +59,7 @@ int main()
6059
char c = 'C';
6160

6261
// number of disks to move in puzzle
63-
size_t num_disk = 4;
62+
size_t num_disk = 3;
6463

6564
// find the total number of moves needed to solve
6665
size_t num_moves = solve_tower(num_disk, a, b, c);

0 commit comments

Comments
 (0)