-
Notifications
You must be signed in to change notification settings - Fork 0
/
tower_of_hanoi.cpp
73 lines (67 loc) · 1.47 KB
/
tower_of_hanoi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <stack> // for std::stack<>
template<typename T>
static void moveDisks(std::size_t n, std::stack<T>& src, std::stack<T>& dst, std::stack<T>& buf)
{
if(n == 0)
return;
// Move n - 1 disks from src to buf using dst as buffer
moveDisks(n - 1, src, buf, dst);
// Move the last disk from src to dst
T value = std::move(src.top());
src.pop();
dst.push(std::move(value));
// Move n - 1 disks from buf to dst using src as buffer
moveDisks(n - 1, buf, dst, src);
}
template<typename T>
static std::stack<T> generateInput()
{
std::stack<T> s;
s.push(100);
s.push(99);
s.push(40);
s.push(20);
s.push(10);
s.push(9);
s.push(8);
s.push(7);
s.push(6);
s.push(5);
s.push(4);
s.push(3);
s.push(2);
s.push(1);
return s;
}
template<typename T>
static std::ostream& operator<<(std::ostream& stream, const std::stack<T> s)
{
stream << "{ [top] ";
std::stack<T> copyS { s };
while(!copyS.empty())
{
stream << copyS.top();
copyS.pop();
if(!copyS.empty())
stream << ", ";
}
stream << " }";
return stream;
}
int main()
{
std::stack<int> src = generateInput<int>();
std::stack<int> dst;
std::stack<int> buf;
std::cout << "Input: \n";
std::cout << "src = " << src << "\n";
std::cout << "dst = " << dst << "\n";
std::cout << "buf = " << buf << "\n";
moveDisks(src.size(), src, dst, buf);
std::cout << "Output: \n";
std::cout << "src = " << src << "\n";
std::cout << "dst = " << dst << "\n";
std::cout << "buf = " << buf << "\n";
return 0;
}