summaryrefslogtreecommitdiff
path: root/introductory_problems/tower_of_hanoi.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-02-17 11:43:13 -0700
committerJack Sangdahl <[email protected]>2025-02-20 12:29:10 -0700
commitdcef6fef864949bcde09907c120180733e5d37bc (patch)
treed36ec0f8dbfdcfb807dbc6e00b4b9923333e379c /introductory_problems/tower_of_hanoi.cpp
parent844264c0bc2777d0e952196ab383f1dc004e6f81 (diff)
downloadcses-dcef6fef864949bcde09907c120180733e5d37bc.tar.gz
cses-dcef6fef864949bcde09907c120180733e5d37bc.zip
2165. tower of hanoi
Diffstat (limited to 'introductory_problems/tower_of_hanoi.cpp')
-rw-r--r--introductory_problems/tower_of_hanoi.cpp21
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);
+}