diff options
Diffstat (limited to 'introductory_problems/tower_of_hanoi.cpp')
-rw-r--r-- | introductory_problems/tower_of_hanoi.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/introductory_problems/tower_of_hanoi.cpp b/introductory_problems/tower_of_hanoi.cpp new file mode 100644 index 0000000..806fc51 --- /dev/null +++ b/introductory_problems/tower_of_hanoi.cpp @@ -0,0 +1,21 @@ +// 2165. Tower of Hanoi +#include <bits/stdc++.h> +using namespace std; + +void move(int from, int to, int depth) { + if (depth == 1) { + printf("%d %d\n", from, to); + return; + } + int other = 6 - from - to; + move(from, other, depth - 1); + printf("%d %d\n", from, to); + move(other, to, depth - 1); +} + +int main() { + static int n; + scanf("%d", &n); + printf("%d\n", (1 << n) - 1); + move(1, 3, n); +} |